| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1616 人关注过本帖
标题:[求助]学继承,发现了个严重的问题,不知道哪错了,大家来看看
只看楼主 加入收藏
绿菜油小苦工
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-10-1
收藏
 问题点数:0 回复次数:13 
[求助]学继承,发现了个严重的问题,不知道哪错了,大家来看看

//这是我看书上的例题改的,原来的程序没有重载>>,没有数据输入函数;
//我改了后问题就出来了....我>>函数居然不能访问类里的数据成员,至少出错提示是这么说的"cannot access......"
//编译器VC6.0
#include <iostream>
#include <string>
using namespace std;
class Student //声明基类
{
public: //基类公用成员
void display( );
friend istream& operator >>(istream& in,Student& stu);
protected : //基类保护成员
int num;
string name;
char sex;
};

void Student::display( )
{
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}

istream& operator >>(istream& in,Student& stu)
{
cout<<"Please input the num,name,and sex: \n";
in>>stu.num>>stu.name>>stu.sex;
return in;
}

class Student1: protected Student //用protected继承方式声明一个派生类
{
public:
void display1( );
friend istream& operator >>(istream& in,Student1& stu);//这里该过来了,谢谢楼下提醒,不过错误提示还是没变;
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;
}
istream& operator >>(istream& in,Student1& stu)
{
cout<<"Please input the num,name,sex,age,and address:\n";
in>>stu.num>>stu.name>>stu.sex>>stu.age>>stu.addr;
return in;
}

int main( )
{
Student1 stud1; //stud1是派生类student1的对象
cout<<"input the student's record:\n";
cin>>stud1;
stud1.display1( ); //display是派生类中的公用成员函数
return 0;
}

--------------------------------------传说中的分割线-------------------------------------------
这里的错误提示:

Compiling...
C5-3.CPP
E:\C++源代码\教材例题程序\C5\C5-3.CPP(25) : error C2248: 'num' : cannot access protected member declared in class 'Student'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(10) : see declaration of 'num'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(25) : error C2248: 'name' : cannot access protected member declared in class 'Student'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(11) : see declaration of 'name'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(25) : error C2248: 'sex' : cannot access protected member declared in class 'Student'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(12) : see declaration of 'sex'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(50) : error C2248: 'num' : cannot access protected member declared in class 'Student'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(10) : see declaration of 'num'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(50) : error C2248: 'name' : cannot access protected member declared in class 'Student'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(11) : see declaration of 'name'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(50) : error C2248: 'sex' : cannot access protected member declared in class 'Student'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(12) : see declaration of 'sex'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(50) : error C2248: 'age' : cannot access private member declared in class 'Student1'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(35) : see declaration of 'age'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(50) : error C2248: 'addr' : cannot access private member declared in class 'Student1'
E:\C++源代码\教材例题程序\C5\C5-3.CPP(36) : see declaration of 'addr'
执行 cl.exe 时出错.

C5-3.OBJ - 1 error(s), 0 warning(s)

谁告诉一下怎么错了?还有,对代码有什么意见都说出来哈,

[此贴子已经被作者于2006-12-6 18:23:21编辑过]

搜索更多相关主题的帖子: 继承 
2006-12-06 14:08
绿菜油小苦工
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-10-1
收藏
得分:0 

今天下午没课,晚上也没课,我在线等:>


java 学习群 38859633 欢迎大家加入
2006-12-06 14:17
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
[CODE]#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
void display( );
friend istream& operator >>(istream& in,Student& stu);
protected :
int num;
string name;
char sex;
};

void Student::display( )
{
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}

istream& operator >>(istream& in,Student& stu)
{
cout<<"Please input the num,name,and sex: \n";
in>>stu.num>>stu.name>>stu.sex;
return in;
}

class Student1: protected Student
{
public:
void display1( );
friend istream& operator >>(istream& in,Student1& stu);
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;
}

istream& operator >>(istream& in,Student1& stu)
{
cout<<"Please input the num,name,sex,age,and address:\n";
cout<<"num = ";
in>>stu.num;
cout<<"name = ";
in>>stu.name;
cout<<"sex('m' or 'w') = ";
in>>stu.sex;
cout<<"age = ";
in>>stu.age;
cout<<"address = ";
getchar();

getline(in, stu.addr);
return in;
}

int main( )
{
Student1 stud1;
cout<<"input the student's record:\n";
cin>>stud1;
stud1.display1( );
return 0;
}[/CODE]

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-12-06 14:50
绿菜油小苦工
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-10-1
收藏
得分:0 

恩?版主,你改了之后还是同样的错误...


java 学习群 38859633 欢迎大家加入
2006-12-06 14:56
绿菜油小苦工
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-10-1
收藏
得分:0 
不过你写的代码让我看的舒服...呵呵

java 学习群 38859633 欢迎大家加入
2006-12-06 14:56
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
你用gcc 编译吧. 如果不会gcc, 到我的博克去看 all about C++

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-12-06 15:02
绿菜油小苦工
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-10-1
收藏
得分:0 
为什么会"cannot access protected member declared in class 'Student'"呢?啊...为什么.

java 学习群 38859633 欢迎大家加入
2006-12-06 15:04
绿菜油小苦工
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-10-1
收藏
得分:0 

哦,我先去装一下GCC


java 学习群 38859633 欢迎大家加入
2006-12-06 15:07
yvtianzll
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-9-13
收藏
得分:0 
[QUOTE]
class Student1: protected Student //用protected继承方式声明一个派生类
{
public:
void display1( );
friend istream& operator >>(istream& in,Student& stu);
private:
int age;
string addr;
};
[/QUOTE]
改成:
[CODE]
class Student1: protected Student //用protected继承方式声明一个派生类
{
public:
void display1( );
friend istream& operator >>(istream& in,Student1& stu);
private:
int age;
string addr;
};
[/CODE]
2006-12-06 15:33
绿菜油小苦工
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-10-1
收藏
得分:0 

回楼上:
版主的那段代码跟你的是一样的,一模一样;输出的结果和我写的出错提示是一样的;还是说不能访问那些数据成员;
还有,GCC我用不来...谁有GCC的帮我编译一下看看是编译器的问题还是我代码的问题;谢谢了;
我真是看不出来怎么就错了;


java 学习群 38859633 欢迎大家加入
2006-12-06 18:16
快速回复:[求助]学继承,发现了个严重的问题,不知道哪错了,大家来看看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027047 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved