0

This is the situation:

int f(int a){
    ...
    g(a);
    ...
}

void g(int a){...}

The problem is that the compiler says that there is no matching function for call to g(int&). It wants me to pass the variable by reference and g() recieves parameters by value.

How can I solve this?

7
  • 1
    What happens if you switch the definitions of f and g round? Commented Nov 7, 2009 at 18:08
  • 1
    Are you sure that's the right code? The function f returns an int, but there is no return statement. Commented Nov 7, 2009 at 18:11
  • can you post the compiler error ? Commented Nov 7, 2009 at 18:11
  • Maybe your function f() doesn't know that the function g() exits. Did you forget to forward declare g()? Or switch f() and g() around like Dominic suggested. Commented Nov 7, 2009 at 18:13
  • 1
    Well the int& in the call doesn't mean you are required to pass a by reference. It's just showing you the arguments you passed, which are g(lvalue int). Since it cannot express lvalue-ness by means of a type, it shows g(int&) instead. Commented Nov 7, 2009 at 18:13

3 Answers 3

12

Well, there's not much here, but the first thing is: make sure you have a declaration for g that's included before f is defined.

void g(int a);

Otherwise, when you get to f, function f has no idea what function g looks like, and you'll run into trouble. From what you've given so far, that's the best I can say.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, it is. If I declared g() as: g(int& a); it would work, but that's not what I want to do.
@macaco: I think you need to post some more code then. What you have posted will not invoke any errors.
1

Your function g() needs to be defined above f()

2 Comments

You mean declared. The compiler looks for the existence of functions, and later links to the actual implementation (the definition).
yes that would also work, but if it is simple function better define it it above
0

There are two things wrong:

1) g is not declared before it is used, so the compiler will compain about that. Assuming you fixed that issue, then the next issue is:

2) f is not returning an int.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.