请在以下方面指导我,我是 C++ 的新手。在这里,我只是在练习 C++ 中的 Class。在编写示例程序时,我只是尝试对其进行一些更改并将函数作为参数传递给另一个函数。
但我认为在适当的程序中我遗漏了一些东西....请指导我...谢谢大家 #包括
using namespace std;
class my_class{
int i;
int j;
public:
int RealNumber(int a, int b);
void WriteAns(int c);
};
int RealNumber(int a, int b)
{
return a+b;
}
void WriteAns(int c)
{
int i = c;
cout<<"Added numbers are: "<<i<<endl;
}
int main()
{
int j =10, k=20;
int m = 0;
m = RealNumber(j,k);
/*I have tried to pass the function RealNumber in place of m,
while declaring the function as follow:
WriteAns(int *c);
and then i have tried to pass function RealNumber as a parameter
WriteAns(&RealNumber(j,k));
*/
WriteAns(m); //By passing m i get the correct ans
return 0;
最佳答案
这是一个非常小的错误;您并没有尝试将实际函数作为参数传递(正如您已经实现的那样),而只是试图传递函数的结果。
将 &RealNumber(j,k)
更改为 RealNumber(j,k)
它应该可以工作
关于c++ - 试图将函数作为参数传递给 C++ 中的另一个函数,但没有得到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898273/