注册 登录
编程论坛 C++教室

多层派生时的构造函数

harbincampus 发布于 2006-12-12 00:12, 587 次点击

小弟这个程序是看的书本上的,用mingw编译就是不通过.
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(int n,string nam )
{
num=n;
name=nam;
}
void display()
{
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
}
protected:
int num;
string name;
};
class Student1:public Student
{
public:
Student1(int n,char nam[10],int a):Student(n,nam)
{
age=a;
}
void show()
{
display();
cout<<"age: "<<age<<endl;
}
private:
int age;
};
class Student2:public Student1
{
public:
Student2(int n,string nam,int a,int s):Student1(n,nam,a)
{
score=s;
}
void show_all()
{
show();
cout<<"score: "<<score<<endl;
}
private:
int score;
};

int main()
{
Student2 stud(10010,"li",17,89);
stud.show_all();

return 0;
}

4 回复
#2
song42006-12-12 09:45
VC6。0正常
你说一下,它说哪错了
#3
harbincampus2006-12-12 10:07

Configuration: 4 - Debug--------------------
Compiling...
item.cpp
item.cpp: In constructor `Student2::Student2(int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int, int)':
item.cpp:40: error: no matching function for call to `Student1::Student1(int&,
std::string&, int&)'
item.cpp:22: error: candidates are: Student1::Student1(const Student1&)
item.cpp:24: error: Student1::Student1(int, char*, int)
item.cpp:58:2: warning: no newline at end of file

item.o - 3 error(s), 1 warning(s)

麻烦老兄了,多谢

#4
song42006-12-12 10:21
Student1(int n,string nam[10],int a):Student(n,nam)
{
age=a;
}
这改一下试试
#5
harbincampus2006-12-12 16:18

版主好厉害,改了之后果然能通过,但是想不通为什么.谢谢啦

1