这是我的代码
#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;
}
如果使用使用
MyTestClass mTC();
相反,它将不允许我调用任何成员函数并显示此错误
./experiment.cpp: 在函数‘int main(int, char**)’中:
./experiment.cpp:31:14:错误:请求成员“firstCallMethod”
‘mTC’,它是非类类型‘MyTestClass()’
我阅读了有关值初始化等的帖子,但这个错误对我来说仍然不合逻辑。为什么这会影响成员函数?
非常感谢帮助:-)
MyTestClass mTC();
不声明一个对象MyTestClass
类,如你所想。
实际上,它声明了一个名为的函数mTC
它不接受任何参数并返回一个MyTestClass
目的。
这被称为最令人烦恼的解析 https://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work in c++.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)