免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: happy_flying
打印 上一主题 下一主题

[C++] 新手请教:为什么编译出错? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-07-31 13:37 |只看该作者
原帖由 Cu_fans 于 2008-7-31 11:40 发表
你的g++竟然连#include 这样的都能通过
我觉得是你编译器太旧的缘故,这里不需要重载赋值操作符的。

不好意思,我贴的编译信息不完整,完整的如下所示:
[root@localhost CH14]# g++ -o test test.cpp
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
                 from test.cpp:1:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
test.cpp: In function `int main()':
test.cpp:31: no matching function for call to `Student::Student(Student)'

test.cpp:13: candidates are: Student::Student(Student&)
test.cpp:7: Student::Student(char* = "no name")
只不过前边那个warning的原因我知道,改成
#include <string.h>
using namespace std;
就可以了.为了突出主要问题,就只贴了我不明白的那部分.

[ 本帖最后由 happy_flying 于 2008-7-31 14:38 编辑 ]

论坛徽章:
1
2017金鸡报晓
日期:2017-01-10 15:19:56
12 [报告]
发表于 2008-07-31 14:05 |只看该作者
operator=

论坛徽章:
0
13 [报告]
发表于 2008-07-31 14:36 |只看该作者
原帖由 net_robber 于 2008-7-31 11:37 发表

那就继续看吧

加快进度

刚看完了,觉得不是运算符重载的事.我做了一下实验,把下面这行
  Student stu1 = fn(randy);

改成如下:
Student stu1 = randy;

或者

Student stu1;
stu1 = fn(randy);
编译就没有问题了.
第三个调用Student的普通构造函数Student(char* pName="no name")生成一个对象stu1,然后调用缺省的赋值函数把函数fn返回的对象赋给stu1.
第一个也就是出错的这个直接调用拷贝构造函数,用函数fn返回的对象去初始化stu1.
第二个也是直接调用拷贝构造函数,用randy去初始化stu1.
那为什么第二个可以而第一个不可以呢?

论坛徽章:
0
14 [报告]
发表于 2008-07-31 15:03 |只看该作者

回复 #1 happy_flying 的帖子

Student& fn(const Student& stu)
     23 {
     24   return stu;
     25 }

论坛徽章:
0
15 [报告]
发表于 2008-07-31 15:18 |只看该作者
原帖由 fangbing007 于 2008-7-31 15:03 发表
Student& fn(const Student& stu)
     23 {
     24   return stu;
     25 }

返回引用的确可以通过编译.
但我更想知道返回值为什么就不能通过编译?

论坛徽章:
0
16 [报告]
发表于 2008-07-31 15:37 |只看该作者
把12行到17行删去就可以了。

有那个函数,就不产生默认的复制构造函数了。
而那个函数有问题。
(与类同名,返回类本身,会被编译器认为是构造函数。  构造函数的参数有这个类,会被认为是复制构造函数,于是。。)

[ 本帖最后由 gtkmm 于 2008-7-31 15:39 编辑 ]

论坛徽章:
0
17 [报告]
发表于 2008-07-31 15:49 |只看该作者
原帖由 happy_flying 于 2008-7-31 14:36 发表

刚看完了,觉得不是运算符重载的事.我做了一下实验,把下面这行
  Student stu1 = fn(randy);

改成如下:
Student stu1 = randy;
或者

Student stu1;
stu1 = fn(randy);编译就没有问题了.
第三个调用 ...

没问题是你运气好

论坛徽章:
0
18 [报告]
发表于 2008-07-31 16:46 |只看该作者
LZ
第12   Student(Student& s)  改为 Student(const Student &s)
就行了

论坛徽章:
0
19 [报告]
发表于 2008-07-31 18:21 |只看该作者

  1. Student stu1 = fn(randy);
复制代码

fn(randy)调用返回一个临时Student对象,然后用该临时Student对象去构造stu1。对于临时对象,只能const引用,而你的拷贝构造函数是Student&,不能够匹配,因而函数解析失败。

更正方法:
Student(Student& s)  --》 Student(const Student &s)

另外,建议:能够写成const引用参数的就不要写成非const引用,能够写成const成员的就不要写成非const成员

论坛徽章:
0
20 [报告]
发表于 2008-07-31 19:35 |只看该作者
D:\MYFILE\c++\test.cpp||In function `int main()'
D:\MYFILE\c++\test.cpp|32|error: no matching function for call to `Student::Student(Student)'|
D:\MYFILE\c++\test.cpp|14|note: candidates are: Student::Student(Student&|
D:\MYFILE\c++\test.cpp|8|note:                 Student::Student(char*)|
||=== Build finished: 1 errors, 0 warnings ===|
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP