这是我的代码
#include <iostream>
using namespace std;
class MyTestClass
{
int MyTestIVar;
public:
MyTestClass(void);
int firstCallMethod(void);
int secondCallMethod(void);
};
MyTestClass::MyTestClass(void)
{
MyTestIVar = 4;
}
int MyTestClass::firstCallMethod(void)
{
return secondCallMethod();
}
int MyTestClass::secondCallMethod(void)
{
return MyTestIVar;
}
int main(int argc, char *argv[])
{
MyTestClass mTC;
cout << mTC.firstCallMethod() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果使用
MyTestClass mTC();
Run Code Online (Sandbox Code Playgroud)
相反,它将禁止我调用任何成员函数并显示此错误
./experiment.cpp:在函数'int main(int,char**)'中:./ .experiment.cpp:31:14:error:请求'mTC'中的成员'firstCallMethod',这是非类型的'MyTestClass()'
我阅读了关于value-initialize等的帖子,但这个错误对我来说似乎不合逻辑.为什么会影响会员功能?
并且非常感谢:-)
MyTestClass mTC();
Run Code Online (Sandbox Code Playgroud)
不要MyTestClass像你想的那样声明类的对象.
实际上,声明一个函数,其名称mTC不带任何参数并返回一个MyTestClass对象.