求解:无法用子类的值初始化基类的引用(非常量限定)

Fear1es5ness 2012-12-08 01:23:15
void show(Info& b)
{
b.display();
}//Info是基类,display是虚函数。

Graduate gs[10];//Graduate是Info派生来的。然后初始化gs,过程就不写了。

show(gs[1]);//这里会对gs[1]报错,错误如标题,这难道是因为Graduate的成员数比Info多两个的原因吗???书上当子类基类成员数一样的时候我看可以直接这么写,没有问题,但是成员数不一样就会报错么?

这个如何解决、是什么原因呢?请告诉我一下,感激不尽。。。
...全文
814 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Fear1es5ness 2012-12-08
  • 打赏
  • 举报
回复
额。。好像加上ifndef后就可以了。。。。。。。。。 这个。。好吧,谢谢了,这么晚你还回帖。
Fear1es5ness 2012-12-08
  • 打赏
  • 举报
回复



有必要逗你玩嘛。确实是这样的,难道是编译器的问题么?我用的VS2010
ri_aje 2012-12-08
  • 打赏
  • 举报
回复
费劲把代码搁在不同文件里了,出现了重定义的编译错误,因为所有的头文件都没有 ifndef 保护,加上以后就没问题了。不过还是无法重现主楼错误,楼主逗我玩呢?
ri_aje 2012-12-08
  • 打赏
  • 举报
回复
先说一下,懒得搞那么多文件,所有代码贴在一个 .cpp 文件里,然后改掉一些明显的打字错误和其他遗漏,编译没有问题,无法重现主楼的错误。
Fear1es5ness 2012-12-08
  • 打赏
  • 举报
回复
其实我想说我没写完。。

//stuInfo.h

#include<string>
#include<iostream>
using namespace std;

class Info{
protected:
	string name;
	string help;
	int math;
	int chinese;
	int english;
public:
	double average;
	Info(string n,string h,int m,int c,int e);
	virtual void display(int swt,ofstream& out) const;
	friend ostream& operator<<(ostream&,Info&);
	friend istream& operator>>(istream&,Info&);
};


/-----------------------------------------------------------------
//stuInfo.cpp


#include"stuInfo.h"
#include"iostream"
#include<string>
#include<fstream>
using namespace std;

Info::Info(string n,string h,int m,int c,int e)
	:name(n),help(h),math(m),chinese(c),english(e){average=(m+c+e)/3;}

void Info::display(int swt,ofstream& out) const{
	if(swt==0){
		cout<<"姓名:"<<name<<",数学:"<<math<<",语文:"<<chinese<<",英语:"<<english<<",平均:"<<average;
	}
	else if(swt==1)
	{
		cout<<"     "<<math;
		out<<"     "<<math;
	}
	else if(swt==2)
	{
		cout<<"     "<<chinese;
	    out<<"     "<<chinese;
	}
	else if(swt==3)
	{
	    cout<<"     "<<english;
		out<<"     "<<english;
	}
	else if(swt==4)
	{
	    cout<<"     "<<average;
		out<<"     "<<average;
	}
	else if(swt==5)
	{
	    cout<<"     "<<help<<endl;
		out<<"     "<<help<<endl;
	}
}
/-----------------------------------------------------


//Student.h

#include"stuInfo.h"
#include<string>
using namespace std;

class Student:public Info{
public:
	Student(string n="Null",string h="Null",int m=0,int c=0,int e=0):Info(n,h,m,c,e){}
	virtual void display(int swt,ofstream& out)const;
    friend ostream& operator<<(ostream&,Student&);
	friend istream& operator>>(istream&,Student&);
};
/---------------------------------------------------------------------------------------

//student.cpp

#include"student.h"
#include"stuInfo.h"
#include<iostream>
#include<string>
using namespace std;

void Student::display(int swt,ofstream& out)const{
	if(swt==0){
		cout<<"学生 "<<name<<" 的信息如下:"<<endl;
		Info::display(swt,out);
	}
	else if(swt==1)
	{
		cout<<" "<<name;
		Info::display(swt,out);
	}
	else if(swt==2)
	{
		cout<<" "<<name;
		Info::display(swt,out);
	}
	else if(swt==3)
	{
		cout<<" "<<name;
		Info::display(swt,out);
	}
	else if(swt==4)
	{
		cout<<" "<<name;
		Info::display(swt,out);
	}
	else if(swt==5)
	{
		cout<<" "<<name;
		Info::display(swt,out);
	}
}

istream& operator>>(istream& in,Student& stu){
	in>>stu.name>>stu.help>>stu.math>>stu.chinese>>stu.english;
}


/-----------------------------------------------------------------------------------

//graduate.h

#include"stuInfo.h"
#include<string>
using namespace std;

class Graduate:public Info{
protected:
	string advisor;
	int classHour;
public:
	Graduate(string n="Null",string h="Null",int m=0,int c=0,int e=0):Info(n,h,m,c,e),advisor("null"),classHour(0){}
	virtual void display(int swt,ofstream& out) const;
	friend ostream& operator<<(ostream&,Graduate&);
	friend istream& operator>>(istream&,Graduate&);
};
/---------------------------------------------------------------------------

//Graduate.cpp


#include"graduate.h"
#include"stuInfo.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

void Graduate::display(int swt,ofstream& out)const{
	if(swt==0)
	{
		cout<<"姓名:"<<name<<",数学:"<<math<<",语文:"<<chinese<<",英语:"<<english<<",平均:"<<average<<",导师:"<<advisor<<",学时"<<classHour<<endl;
	}
	else if(swt==1)
	{
		cout<<"     "<<math;
		out<<"     "<<math;
	}
	else if(swt==2)
	{
		cout<<"     "<<chinese;
		out<<"     "<<chinese;
	}
	else if(swt==3)
	{
	    cout<<"     "<<english;
		out<<"     "<<english;
	}
	else if(swt==4)
	{
	    cout<<"     "<<average;
		out<<"     "<<average;
	}
	else if(swt==5)
	{
	    cout<<"     "<<help;
		out<<"     "<<help;
	}
	else if(swt==6)
	{
		cout<<"     "<<advisor;
		out<<"     "<<advisor;
	}
	else if(swt==7)
	{
		cout<<"     "<<classHour<<endl;
		out<<"     "<<classHour<<endl;
	}
}

istream& operator>>(istream& in,Graduate& gs){
	in>>gs.name>>gs.help>>gs.math>>gs.chinese>>gs.english>>gs.advisor>>gs.classHour;
}

/-----------------------------------------------------------------------------------------


//执行.cpp

#include<string>
#include<iostream>
#include<fstream>
#include"graduate.h"
#include"student.h"
#include"stuInfo.h"
using namespace std;

void show(Info& b,int swt,ofstream& out)
	{
		b.display(swt,out);
	}

int main()
{
	ifstream in("信息.txt");
	ofstream out("输出.txt");
	int i=0,j=0;
	string label;
	string type="Null";
	Student stu[20];
	Graduate gs[20];
	getline(in,label);//让文件换行,以便后面输入正文,文件格式大概是第一行为姓名 各科成绩之类的文字标签,然后从第二行开始是各个对象包含的内容。


	for(int m=0;m<100;m++)
	{
		in>>type;
	    if(type=="本科生") 
			in>>stu[i],i++;
		else if(type=="研究生")in>>gs[j],j++;
		else break;
	}//这段顺便帮看看,我不太确定我的输入操作符重载是否正确。

	show(stu[1],1,out);//测试而已,没什么含义。

[code=c]
好吧,编不下去了。。。。正好在文件输入那有点小问题还没确定,顺便帮忙看看有没有问题吧。。。
ri_aje 2012-12-08
  • 打赏
  • 举报
回复
上全代码。。

64,637

社区成员

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

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