C ++:按值传递变量(C++: pass variable by value)

情况就是这样:

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

问题是编译器说没有匹配函数来调用g(int&)。 它希望我通过引用传递变量,g()按值接收参数。

我怎么解决这个问题?

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?

最满意答案

嗯,这里没什么,但首先是:确保你在定义f之前有一个g的声明。

void g(int a);

否则,当你到f时,函数f不知道g看起来是什么函数,你会遇到麻烦。 从你到目前为止所给出的,这是我能说的最好的。

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.

C ++:按值传递变量(C++: pass variable by value)

情况就是这样:

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

问题是编译器说没有匹配函数来调用g(int&)。 它希望我通过引用传递变量,g()按值接收参数。

我怎么解决这个问题?

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?

最满意答案

嗯,这里没什么,但首先是:确保你在定义f之前有一个g的声明。

void g(int a);

否则,当你到f时,函数f不知道g看起来是什么函数,你会遇到麻烦。 从你到目前为止所给出的,这是我能说的最好的。

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.