#include <iostream>#include <string.h>using namespace std;class Internet{ public: Internet(char *name, char *address) { cout<<"load the construction function"<<endl; strcpy(Internet::name, name); } Internet(Internet &temp) { cout<<"load COPY construction function"<<endl; strcpy(Internet::name, temp.name); } ~Internet() { cout<<"load destruction function"<<endl; } protected: char name[50]; char address[30];};int main(){ Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com"); cout<<a.name<<endl; return 0;}
出现错误: cc4.cpp: In function ‘int main()’: cc4.cpp:30: warning: deprecated conversion from string constant to ‘char*’ cc4.cpp:30: warning: deprecated conversion from string constant to ‘char*’ cc4.cpp:30: error: no matching function for call to ‘Internet::Internet(Internet)’ cc4.cpp:14: note: candidates are: Internet::Internet(Internet&) cc4.cpp:9: note: Internet::Internet(char*, char*) cc4.cpp:24: error: ‘char Internet::name [50]’ is protected cc4.cpp:31: error: within this context
请问是为什么? 谢谢!
[解决办法] 1,第二十行: name 是protected类型 不能再main里直接访问的。
2, Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com");
复制构造函数,我看不出有什么问题。。
[解决办法] Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com"); 这样写会调用拷贝构造函数,但由于你建了一个临时变量,不能作为左值,也就不能匹配Internet(Internet &temp) 你把Internet(Internet &temp)改成Internet(const Internet &temp)就可以了