免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 4452 | 回复: 23
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-31 11:04 |只看该作者 |倒序浏览
      1 #include <iostream.h>
      2 #include <string.h>
      3
      4 class Student{
      5 public:
      6   Student(char* pName="no name")
      7   {
      8     strncpy(name,pName,sizeof(name));
      9     name[sizeof(name)-1]='\0';
     10   }
     11
     12   Student(Student& s)
     13   {
     14     strcpy(name,"copy of ");
     15     strcat(name,s.name);
     16   }
     17
     18 protected:
     19   char name[40];
     20 };
     21
     22 Student fn(Student stu)
     23 {
     24   return stu;
     25 }
     26
     27 int main()
     28 {
     29   Student randy("Randy");
     30
     31   Student stu1 = fn(randy);
     32
     33   return 0;
     34 }
     35

编译出错信息如下:
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")

刚开始学c++,还请大家多多帮忙.

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

论坛徽章:
0
2 [报告]
发表于 2008-07-31 11:12 |只看该作者
貌似没什么错误啊。
不过你最好这样写头文件:
#include <iostream>
#include <string>
using namespace std;

还有最好把这两个位置改下:
Student(const Student& s)
Student fn(const Student &stu)

论坛徽章:
0
3 [报告]
发表于 2008-07-31 11:16 |只看该作者
没有进行操作符重载

论坛徽章:
0
4 [报告]
发表于 2008-07-31 11:20 |只看该作者
原帖由 Cu_fans 于 2008-7-31 11:12 发表
貌似没什么错误啊。
不过你最好这样写头文件:
#include
#include
using namespace std;

还有最好把这两个位置改下:
Student(const Student& s)
Student fn(const Student &stu)


我这儿就是编译错误呀.
我是在linux下用g++来编译的:
g++ -o test test.cpp

论坛徽章:
0
5 [报告]
发表于 2008-07-31 11:34 |只看该作者
原帖由 net_robber 于 2008-7-31 11:16 发表
没有进行操作符重载

呵呵,还没有看到运算符重载那章,能否请版主说详细一点.

论坛徽章:
0
6 [报告]
发表于 2008-07-31 11:34 |只看该作者
我用DEV Cpp来编译的没错。这个对C++标准来说还是执行得很好的。

实在不行的话你再写个 重载 赋值操作符的函数试试吧。

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

呵呵,还没有看到运算符重载那章,能否请版主说详细一点.

那就继续看吧

加快进度

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

论坛徽章:
0
9 [报告]
发表于 2008-07-31 11:48 |只看该作者
我的这 也编译不通过
但 经过2楼所说那样改后就能能通过了
但是我又觉得 楼主的 貌似没有问题啊...
看不出来哪里出现问题了....

论坛徽章:
0
10 [报告]
发表于 2008-07-31 12:45 |只看该作者

回复 #8 Cu_fans 的帖子

这个include又什么问题吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP