抽象类的运算符重载在继承时遇到问题

Scorpiour 2012-05-07 11:10:37

using namespace std;

void displayInfo();

class MEDIA
{
protected:
int ID;
string Title;
int Year;
public:
MEDIA()
{
ID=0;
Title="";
Year=0;
}
virtual ~MEDIA(){}
virtual void changeID(int)=0;
virtual void Print(ostream&)const=0;
friend ostream& operator <<(ostream&,const MEDIA&);
};

ostream& operator <<(ostream out,const MEDIA& M)
{
M.Print(out);
return out;
}

class DVD:public MEDIA
{
private:
string Director;
public:
DVD(int newID,const char* newTitle,int newYear,const char* name="Unknown")
{
ID=newID;
Title=newTitle;
Year=newYear;
Director=name;
}

virtual void changeID(int newID)
{
ID=newID;
}

virtual void Print(ostream& out)const
{
out<<"DVD: ID"<<ID<<"\tTitle "<<Title<<"\t("<<Year<<")\tDirected by "<<Director<<endl;
}

};


class Book:public MEDIA
{
private:
string Author;
int NumPage;
public:
Book(int newID,const char* newTitle,int newYear,const char* name,int page)
{
ID=newID;
Title=newTitle;
Year=newYear;
Author=name;
NumPage=page;
}

virtual void changeID(int newID)
{
ID=newID;
}

virtual void Print(ostream& out)const
{
out<<"Book: ID"<<ID<<"\tTitle "<<Title<<"\t("<<Year<<")\t by "<<Author<<",\t"<<NumPage<<" pages"<<endl;
}
};



class Journal:public MEDIA
{
private:
int Volumn;
int Number;
public:
Journal(int newID,const char* newTitle,int newYear,int newVol,int num)
{
ID=newID;
Title=newTitle;
Year=newYear;
Volumn=newVol;
Number=num;
}

virtual void changeID(int newID)
{
ID=newID;
}

virtual void Print(ostream& out)const
{
out<<"Journal: ID"<<ID<<"\tTitle "<<Title<<"\t("<<Year<<")\t Volumn: "<<Volumn<<",\tNumber"<<Number<<endl;
}
};




int main()
{
MEDIA *ptr[10];

ptr[0] = new DVD(352, "Remember The Alamo", 1945, "George Smith");
ptr[1] = new DVD(831, "High School Blues", 1984);
ptr[2] = new DVD(194, "Going For The Touchdown", 1984, "Frank Madden");
ptr[3] = new DVD(576, "Martian Hairdresser", 1992, "Debbie Gold");
ptr[4] = new Book(608,"How to Make Money", 1987, "Phil Barton", 324);
ptr[5] = new Book(442,"Garden Projects At Home", 1998, "Mary Freeman", 164);
ptr[6] = new Book(185,"The Haunted House Mystery", 1996, "Bert Morgan", 53);
ptr[7] = new Journal(294, "ACM", 2009, 6, 8);
ptr[8] = new Journal(521, "J of Logic", 2008, 23, 14);
ptr[9] = new Journal(630, "J of AI", 2009, 35, 11);

cout << "Printing 10 items..." << endl << endl;

for (int i = 0; i < 10; ++i)
{
cout << *ptr[i] << endl;
}

ptr[3]->changeID(707);
ptr[5]->changeID(808);
ptr[7]->changeID(909);

cout << endl << "Printing again..." << endl << endl;
cout << *ptr[3] << endl;
cout << *ptr[5] << endl;
cout << *ptr[7] << endl;

return 0;
}



编译时显示 error: ambiguous overload for 'operator<<

但是我确实只有一个friend函数阿……想不明白问题所在中……求助~
...全文
166 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
firendlys 2012-05-07
  • 打赏
  • 举报
回复
一个 friend 函数吗?

{
protected:
int ID;
string Title;
int Year;
public:
MEDIA()
{
ID=0;
Title="";
Year=0;
}
virtual ~MEDIA(){}
virtual void changeID(int)=0;
virtual void Print(ostream&)const=0;
friend ostream& operator <<(ostream&,const MEDIA&);// here , ostream &
};

ostream& operator <<(ostream out,const MEDIA& M)// 没有 &
{
M.Print(out);
return out;
}


其他代码没看,也没去调试,所以有没有其他错误,就不知道了。
Scorpiour 2012-05-07
  • 打赏
  • 举报
回复
非常感谢。。。最近各种低级错误
大尾巴猫 2012-05-07
  • 打赏
  • 举报
回复
ostream& operator <<(ostream out,const MEDIA& M)
{
M.Print(out);
return out;
}

参数里面 ostream & out吧,漏了个&
Scorpiour 2012-05-07
  • 打赏
  • 举报
回复
如果把函数

ostream& operator <<(ostream&,MEDIA&)

放在代码后面的话,虽然可以编译通过,但是linker依然报错
(.text+0xa30): undefined reference to `operator<<(std::ostream&, MEDIA const&)'

无法理解中

64,685

社区成员

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

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