求教一个问题,自学《30天掌握C++精髓》遇到的

frogoscar
企业官方账号
2012-06-23 03:33:50
小弟最近开始在看《30天掌握C++精髓》pdf
第101页的代码,在Ubuntu下用g++编译

#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

请问是为什么?
谢谢!
...全文
247 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
frogoscar 2012-06-28
  • 打赏
  • 举报
回复
可能,估计那个书的作者用的不是g++(gcc)。
谢谢啦
lyyscf 2012-06-25
  • 打赏
  • 举报
回复
Internet &a = Internet("中国软件开发lab", "www.cndev-lab.com");
这个时候 a引用的是个临时变量
g++中对于临时变量控制的比较严格
vc里面,引用貌似能够延长临时对象的生存期

g++可能认为不合法,给禁止了
frogoscar 2012-06-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

Internet(const Internet &temp)
[/Quote]
代码里已经
Internet(const Internet &temp)
了啊。
frogoscar 2012-06-24
  • 打赏
  • 举报
回复

Internet(const Internet &temp)
{
cout<<"load COPY construction function"<<endl;
strcpy(Internet::name, temp.name);
}

那个拷贝构造函数本来就是这样的啊。
已经有const了。
qq120848369 2012-06-23
  • 打赏
  • 举报
回复
Internet(const Internet &temp)
frogoscar 2012-06-23
  • 打赏
  • 举报
回复
在请教一个问题:
他下面又有个例子,说是测试引用无名对象的:

#include <iostream>
#include <string.h>

using namespace std;

class Internet
{
public:
Internet(const char *name, const char *address)
{
cout<<"load the construction function"<<endl;
strcpy(Internet::name, name);
}
Internet(const Internet &temp)
{
cout<<"load COPY construction function"<<endl;
strcpy(Internet::name, temp.name);
}
~Internet()
{
cout<<"load destruction function"<<endl;
}
public:
char name[50];
char address[30];
};

int main()
{
Internet &a = Internet("中国软件开发lab", "www.cndev-lab.com");
cout<<a.name<<endl;

return 0;
}

但编译出现错误:
cc5.cpp:30: error: invalid initialization of non-const reference of type ‘Internet&’ from a temporary of type ‘Internet’

我改成这样就好了:

#include <iostream>
#include <string.h>

using namespace std;

class Internet
{
public:
Internet(const char *name, const char *address)
{
cout<<"load the construction function"<<endl;
strcpy(Internet::name, name);
}
Internet(const Internet &temp)
{
cout<<"load COPY construction function"<<endl;
strcpy(Internet::name, temp.name);
}
~Internet()
{
cout<<"load destruction function"<<endl;
}
public:
char name[50];
char address[30];
};

int main()
{
Internet b("中国软件开发lab", "www.cndev-lab.com");
Internet &a = b;
cout<<a.name<<endl;

return 0;
}


为什么呢?引用不能接受无名对象的赋值吗?请指教,谢谢!
bearg 2012-06-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 4 楼 的回复:

引用 1 楼 的回复:

1,第二十行:
name 是protected类型
不能再main里直接访问的。

2,
Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com");

复制构造函数,我看不出有什么问题。。

那怎么能在main里访问name变量?
难道改成public?
谢……
[/Quote]
改成public就好啦
frogoscar 2012-06-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 1 楼 的回复:

1,第二十行:
name 是protected类型
不能再main里直接访问的。

2,
Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com");

复制构造函数,我看不出有什么问题。。

那怎么能在main里访问name变量?
难道改成public?
谢谢
[/Quote]
是改成public
frogoscar 2012-06-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

哥哥,你是学java的吧?
[/Quote]
恩,java和c,
c++确实了解不深啊,需要努力。。。
frogoscar 2012-06-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

换本好点的教材吧。30天?3年还勉强。
[/Quote]
恩,感觉这书有点水。100页之前的例子还是不错的。
当然我要学C++,不会只看这一本的啦,也同时在看 C++ primer fourth edition和C++语言的设计和演化
youkuxiaobin 2012-06-23
  • 打赏
  • 举报
回复
哥哥,你是学java的吧?
cryingbee 2012-06-23
  • 打赏
  • 举报
回复
Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com");
这样写会调用拷贝构造函数,但由于你建了一个临时变量,不能作为左值,也就不能匹配Internet(Internet &temp)
你把Internet(Internet &temp)改成Internet(const Internet &temp)就可以了

还有那些warning,也是没有加const引起的
构造函数最好改成Internet(const char *name, const char *address)

在c++中const会引起很多编译错误,所以写的时候还是要规范,该加const的时候就应该加。
2012-06-23
  • 打赏
  • 举报
回复
1,第二十行:
name 是protected类型
不能再main里直接访问的。

2,
Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com");

复制构造函数,我看不出有什么问题。。
taodm 2012-06-23
  • 打赏
  • 举报
回复
换本好点的教材吧。30天?3年还勉强。
frogoscar 2012-06-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

1,第二十行:
name 是protected类型
不能再main里直接访问的。

2,
Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com");

复制构造函数,我看不出有什么问题。。
[/Quote]
那怎么能在main里访问name变量?
难道改成public?
谢谢
frogoscar 2012-06-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

Internet a = Internet("中国软件开发实验室", "www.cndev-lab.com");
这样写会调用拷贝构造函数,但由于你建了一个临时变量,不能作为左值,也就不能匹配Internet(Internet &temp)
你把Internet(Internet &temp)改成Internet(const Internet &temp)就可以了
……
[/Quote]
太感谢了,加了const果然没错误,也没那个warning了。
C++果然博大精深,好复杂啊。我得努力才行。
不愧是相对最难的编程语言。。。

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧