c/c++语言开发共享类———用类定义对象———error:C++表达式必须包含类类型

//原文参考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 }

 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/603429.html

(0)
上一篇 2021年5月11日
下一篇 2021年5月11日

精彩推荐