我是 C++ 新手,犯了初学者的错误:
myclass.cpp: In function ‘int main()’:
myclass.cpp: 14:16: error: ‘func’ was not declared in this scope
这是代码:
#include <iostream>
using namespace std;
class MyClass{
public:
int func(int);
};
int MyClass::func(int a){
return a*2;
}
int main(){
cout << func(3);
}
我希望你能帮助我。
最佳答案
int main(){
cout << func(3);
}
func
不是全局函数;它是类的成员函数。您需要类的一个实例才能访问它。
例如:
int main()
{
MyClass obj;
std::cout<< obj.func(3);
}
关于c++ - 函数 "was not declared in this scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16136551/