欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

【C++】error C2512: 'Adder' : no appropriate default constructor available

发布时间:2025/3/21 c/c++ 61 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【C++】error C2512: 'Adder' : no appropriate default constructor available 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、出现错误的代码

#include <iostream> using namespace std;class Adder{ public:// 构造函数Adder(int i){total = i;}// 对外的接口void addNum(int number){total += number;}// 对外的接口int getTotal(){return total;}; private:// 对外隐藏的数据int total; }; int main( ) {Adder a;a.addNum(10);a.addNum(20);a.addNum(30);cout << "Total " << a.getTotal() <<endl;return 0; }

2、两种修改方法

#include <iostream> using namespace std;class Adder{ public:// 构造函数Adder(int i = 0){total = i;}// 对外的接口void addNum(int number){total += number;}// 对外的接口int getTotal(){return total;}; private:// 对外隐藏的数据int total; }; int main( ) {Adder a;a.addNum(10);a.addNum(20);a.addNum(30);cout << "Total " << a.getTotal() <<endl;return 0; } #include <iostream> using namespace std;class Adder{ public:// 构造函数Adder(int i){total = i;}// 对外的接口void addNum(int number){total += number;}// 对外的接口int getTotal(){return total;}; private:// 对外隐藏的数据int total; }; int main( ) {Adder a(0);a.addNum(10);a.addNum(20);a.addNum(30);cout << "Total " << a.getTotal() <<endl;return 0; }

正确结果:

总结

以上是生活随笔为你收集整理的【C++】error C2512: 'Adder' : no appropriate default constructor available的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。