//原文参考https://blog.csdn.net/lanchunhui/article/details/52503332
你以为你定义了一个类的对象,其实在编译器看来你是声明了一个函数
1 class Test{ 2 public: 3 Test(){ }//无参构造函数 4 void fool(){ } 5 }; 6 int main(){ 7 Test t(); // 编译器会将 t 视为一个函数; 8 t.fool(); // 出错:C++表达式必须包含类类型 9 return 0; 10 }
修改为:
1 //对象的定义,修改为: 2 Test t;
当构造函数中存在一些参数时:
1 class Test{ 2 public: 3 Test(int i) {} 4 ... 5 }; 6 int main(){ 7 Test t(5); 8 ... 9 }
当构造函数的参数带默认值:
1 class Test{ 2 Test(int i = 0) {} 3 }; 4 int main(){ 5 Test t;//此时i为默认值 6 Test t(1);//此时i为1 7 Test t();//此时报错:C++ 表达式必须包含类类型 8 ... 9 }