求解一个C++问题 error C2629: unexpected 'class student1 ('

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

class student1:public student
{public:
student1(int n,sting nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)
{age=a;
adr=ad;
}
void display()
{cout<<"the student is :"<<show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.display();
}
private:
student monitor;
int age;
string adr;
};
int main()
{student1 stu1(10010,"wang-li",10010,"li-sun",19," 115 beijing road ,shanghai");
stu1.display();
stu1.display_monitor();
return 0;
}

我帮你输到VC里面试了试。
有这么些错误:
1.student1(int n,sting nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)第二个参数string写错了
2.cout<<show()不接受这么写法,删除本行或者你直接show()方法里面写显示数据
3.student作为基类,没有display()方法,你使用了基类去调用派生类的方法,这是错误的
以下程序通过编译。另外,纯手工打造的回答,LZ务必选为最佳,否则让热心的答题人心寒啊,谢谢!

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

class student1:public student
{public:
student1(int n,string nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)
{
age=a;
adr=ad;
}
void display()
{
//cout<<"the student is :"<<show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
//monitor.display();
}
private:
student monitor;
int age;
string adr;
};
int main()
{
student1 stu1(10010,"wang-li",10010,"li-sun",19," 115 beijing road ,shanghai");
stu1.display();
stu1.display_monitor();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-16
#include<iostream>
#include<string>
using namespace std;
class student
{public:
student(int n,string nam)
{num=n;
name=nam;
}
void show()
{cout<<"num"<<num<<" "<<name<<endl;}
protected:
int num;
string name;
};

class student1:public student
{public:
student1(int n,sting nam,int n1,string nam1,int a,string ad):student(n,nam),monitor(n1,nam1)
{age=a;
adr=ad;
}
void display()
{cout<<"the student is :"<<show();//show返回void,不能输出到cout
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.display();//monitor是student类型,student类没有dispaly()成员函数
}
private:
student monitor;
int age;
string adr;
};
int main()
{student1 stu1(10010,"wang-li",10010,"li-sun",19," 115 beijing road ,shanghai");
stu1.display();
stu1.display_monitor();
return 0;
}
第2个回答  2011-04-16
错误代码部分:
void display()
{cout<<"the student is :"<<show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.display();
}
修改结果:
void display()
{cout<<"the student is :"<<endl;
this.show();
cout<<age<<" : "<<adr<<endl;
}
void display_monitor()
{
cout<<endl<<"the monitor is:"<<endl;
monitor.show();
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网