这是什么地方错了呢?

harderror 2012-08-09 02:21:25

#include <iostream>
#include <string>
using namespace std;
class student
{public:
student(int n,string nam,char se)
{num=n;
name=nam;
sex=se;
}
void display();
protected:
int num;
string name;
char sex;
};
void student::display()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}

class student1:protected student
{public:
student1(int n,string nam,char se,int ag,string add):student(n,nam,se)
{age=ag;
addr=add;
}
void display1();
private:
int age;
string addr;
};
void student1::display1()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
int main()
{student stu(1001,"wuminglu","m");
student1 stu1(26,"dalian");
stu.display();
stu1.display1();
return 0;
}





...全文
57 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
harderror 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

student stu(1001,"wuminglu","m");
student1 stu1(26,"dalian");
创建对象时需要构造函数,你没发现你的两个构造函数在类里面找不到对应的构造函数嘛?
正确的是:
student stu(1001,"wuminglu",'m');
student1 stu1(1001,"wuminglu",'m',26,"dalian");
[/Quote]

谢谢你了,不过已经结贴了.
Joseph-Growth 2012-08-09
  • 打赏
  • 举报
回复
student stu(1001,"wuminglu","m");
student1 stu1(26,"dalian");
创建对象时需要构造函数,你没发现你的两个构造函数在类里面找不到对应的构造函数嘛?
正确的是:
student stu(1001,"wuminglu",'m');
student1 stu1(1001,"wuminglu",'m',26,"dalian");
harderror 2012-08-09
  • 打赏
  • 举报
回复
谢谢了.
lylm 2012-08-09
  • 打赏
  • 举报
回复
对比一下注释的地方
#include <iostream>
#include <string>
using namespace std;
class student
{public:
student(int n,string nam,char se)
{num=n;
name=nam;
sex=se;
}
void display();
protected:
int num;
string name;
char sex;
};
void student::display()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}

class student1:protected student
{public:
student1(int n,string nam,char se,int ag,string add):student(n,nam,se)
{age=ag;
addr=add;
}
void display1();
private:
int age;
string addr;
};
void student1::display1()
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
int main()
{student stu(1001,"wuminglu",'m');//改成单引号
student1 stu1(26,"dalian",'f',15,"test");//构造函数需要5个参数
stu.display();
stu1.display1();
return 0;
}

IVERS0N 2012-08-09
  • 打赏
  • 举报
回复
student stu(1001,"wuminglu",m); //第三参数是字符

student1 stu1(26,"dalian");//这个构造函数5个参数

64,402

社区成员

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

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