帮忙看一下错误怎么改

ImJaden 2010-03-30 09:11:55
我没有按书上的原意写的,还是重载了输出,可提示的错误我不知道怎么改。



#include<iostream.h>
class Student1
{
public:
void get_value()
{
cin>>num>>name>>sex;
}
friend ostream& operator<<(ostream &out,Student1 &stud)
{
out<<"number:\n"<<stud.num<<"name:\n"<<stud.name<<"sex:\n"<<stud.sex<<endl;
return out;
}
private:
int num;
char name[20];
char sex;
};
/*ostream& operator<<(ostream &out,Student1 &stud)
{
out<<"number:\n"<<stud.num<<"name:\n"<<stud.name<<"sex:\n"<<stud.sex<<endl;
return out;
}*/

class Student2:public Student1
{
public:
void get_value_1()
{
get_value();
cin>>age>>addr;
cout<<endl;
}
friend ostream& operator<<(ostream&,Student2)
{
out<<"age:\n"<<stud.age<<"addr:\n"<<stud.addr;
return out;
}
protect:
int age;
char addr[30];
};
/*ostream& operator<<(ostream &out,Student2 &stud)
{
out<<"age:\n"<<stud.age<<"addr:\n"<<stud.addr;
return out;
}*/
int main()
{
Student2 stud;
cout<<"input the Student's number,name,sex,age,addr:\n";
stud.get_value_1();
cout<<stud;
return 0;
}

Compiling...
public.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\2010\public.cpp(40) : error C2062: type 'char' unexpected
C:\Program Files\Microsoft Visual Studio\MyProjects\2010\public.cpp(40) : error C2238: unexpected token(s) preceding ';'
Error executing cl.exe.

public.obj - 2 error(s), 0 warning(s)
...全文
114 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ImJaden 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cy330206 的回复:]
引用 2 楼 junjie9002 的回复:
没using namespace std;还怎么编译啊

这个要看编译器的
[/Quote]
vc 6.0提供的头文件不支持运载符重载为友元函数,所以要用#include<iostream.h>
ImJaden 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gigglesun 的回复:]
#include<iostream.h>
改成
#include<iostream>
using namespace std;
[/Quote]
vc 6.0提供的头文件不支持运载符重载为友元函数,所以要用#include<iostream.h>
ImJaden 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 junjie9002 的回复:]
没using namespace std;还怎么编译啊
[/Quote]
vc 6.0提供的头文件不支持运载符重载为友元函数,所以要用#include<iostream.h>
lzh9955 2010-03-30
  • 打赏
  • 举报
回复
protected: //关键字错了
cy330206 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 junjie9002 的回复:]
没using namespace std;还怎么编译啊
[/Quote]
这个要看编译器的
ithiker 2010-03-30
  • 打赏
  • 举报
回复
#include<iostream.h>
改成
#include<iostream>
using namespace std;
djh512 2010-03-30
  • 打赏
  • 举报
回复

#include<iostream.h>
class Student1
{
public:
void get_value()
{
cin>>num>>name>>sex;
}
friend ostream& operator<<(ostream &out,Student1 &stud)
{
out<<"number:\n"<<stud.num<<"name:\n"<<stud.name<<"sex:\n"<<stud.sex<<endl;
return out;
}
private:
int num;
char name[20];
char sex;
};

class Student2:public Student1
{
public:
void get_value_1()
{
get_value();
cin>>age>>addr;
cout<<endl;
}
friend ostream& operator<<(ostream & out,Student2 stud)//形参要给出
{
out<<"age:\n"<<stud.age<<"addr:\n"<<stud.addr;
return out;
}
protected: //关键字错了
int age;
char addr[30];
};
int main()
{
Student2 stud;
cout<<"input the Student's number,name,sex,age,addr:\n";
stud.get_value_1();
cout<<stud;
return 0;
}

junjie9002 2010-03-30
  • 打赏
  • 举报
回复
没using namespace std;还怎么编译啊
junjie9002 2010-03-30
  • 打赏
  • 举报
回复
没using namespace std;还怎么编译啊
jbz001 2010-03-30
  • 打赏
  • 举报
回复
你这到处是书写错误啊!!!
好好检查一下你的关键词和名称

64,683

社区成员

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

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