首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-05-09 
抽象类的运算符重载在继承时遇到问题C/C++ codeusing namespace stdvoid displayInfo()class MEDIA{prot

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

C/C++ code
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函数阿……想不明白问题所在中……求助~

[解决办法]
ostream& operator <<(ostream out,const MEDIA& M)
{
M.Print(out);
return out;
}

参数里面 ostream & out吧,漏了个&

热点排行