Gerardo
Gerardo

Reputation: 1948

C++: pass variable by value

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?

Upvotes: 0

Views: 236

Answers (3)

Kyle Walsh
Kyle Walsh

Reputation: 2774

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.

Upvotes: 0

Ponting
Ponting

Reputation: 1006

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

Upvotes: 1

Michael Kohne
Michael Kohne

Reputation: 12044

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.

Upvotes: 12

Related Questions