【C++】error C2512: 'Adder' : no appropriate default constructor available
生活随笔
收集整理的这篇文章主要介绍了
【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的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【C++】not accessible
- 下一篇: 【C/C++】将二个有序数组合并