2011-11-21 47 views
1

當我運行此操作時,我不斷收到以下錯誤。任何幫助?運營商的ambigious過載>>'在'getfile >>點'

ambigious overload for ‘operator >> ’ in ‘getfile >> point’ 

這是我在第對象代碼

istream& pp::operator >> (istream& in) 
{ 
    //error msg 
    string msg = "Error. Output from file, String is found instead of integer. Please check file for errors\n"; 

    //ignore everything till next record 
    in.ignore(256, '['); 

    //store x cordinate 
    in >> x; 
    if(in.fail()){throw msg;} 

    //ignore everything till next record 
    in.ignore(256, ','); 

    //store y cordinate 
    in >> y; 
    if(in.fail()){throw msg;} 

    //ignore the rest of the records till next line 
    in.ignore(256, '\n'); 

    return in; 

}//end of operator >> method 

這是我的主要

{ 
    ifstream getfile; 
    //get filename to input data into system 
    cout << "\nPlease enter filename : "; 
    cin >> file; 
    getfile.open(file, ios::in); 

    pp pointObject(); 
    getfile >> point; 
} 

這是我pp.h結構,其中你們希望看到的。

class pp 
    { 
    public: 
    //constructor 
    pp(); 
    pp(int, int); 
    pp(const Point2D &pd); 

    //operator overloaded methods 
    virtual pp operator-(pp pd); 
    virtual bool operator<(pp pd) const; 
    virtual bool operator>(pp pd) const; 
    virtual bool operator==(pp pd); 
    virtual ostream& operator<<(ostream& o) const; 
    virtual istream& operator>>(istream& in;  

    //accessor,mutator methods 
    int getX() const; 
    int getY() const; 

    void setX(int); 
    void setY(int); 

    protected: 
    int x; 
    int y; 
    }; 
    #endif 
+1

什麼是'getfile'? – Kos

+0

顯示'點'的定義。 – trojanfoe

+1

雖然與你的問題無關,你真的不應該'拋出'字符串。使用其中一個標準異常,比如'runtime_error',構造函數接受一個字符串作爲參數,這將由'exception :: what'函數返回。 –

回答

3

operator>>不能是類的成員,因爲這樣可以給它以錯誤的順序的參數。您必須考慮所有成員函數(和運算符)的隱含this參數。

簽名應該是istream& operator>>(istream& in, pp& point),以便在getfile >> point;中使用它。

+0

奇怪的事情,當我做了以上的以下,我不斷收到在頭文件中的以下錯誤。錯誤:'std :: istream&pp :: operator >>(std :: istream&,pp&)'必須採用一個參數 – mister

+0

如果它是一個成員,則有'this'參數和'istream'。這使得它有兩個參數,這是操作員使用的(並且編譯器知道的)。如果你想要或者需要在課堂上申報,你可以把它變成課堂的朋友。 '朋友istream&operator >>(istream&in,pp&point)'會好的。 –

+0

謝謝隊友!乾杯! – mister

0

的定義您operator >>應該是:

istream& operator>>(istream& in, pp &p) 

然後,在函數體,你應該指定讀p.xp.yxy值。

+0

奇怪的事情,當我做了以上的以下,我不斷收到在頭文件中的以下錯誤。 錯誤:「的std :: istream的&頁::運算符>>(的std :: istream的&,PP&)」必須只有一個參數 – mister

+0

它是一個免費的功能,而不是一個'pp'成員函數。 –

0

我建議寫一個獨立的運算符重載

注意也回覆:處理流錯誤和使用異常,請參閱try/catch throws error

friend istream& pp::operator>> (istream& in, pp& pointObject) 
{ 
    //error msg 
    const string msg = "ErPoint2Dror. Output from file, String is found instead of integer. Please check file for errors\n"; 

    //ignore everything till next record 
    in.ignore(256, '['); 

    int x,y; 

    //store x cordinate 
    in >> x; 
    if(in.fail()) 
    { 
     throw msg; 
    } 

    //ignore everything till next record 
    in.ignore(256, ','); 

    //store y cordinate 
    in >> y; 
    if(in.fail()) 
    { 
     throw msg; 
    } 

    //ignore the rest of the records till next line 
    in.ignore(256, '\n'); 

    pointObject.x = x; 
    pointObject.y = y; 

    return in; 
}//end of operator >> method 
+0

您好我不能使用的朋友,因爲我有其他4類在那裏我有不同勢函數重載同一個運營商! :D – mister

0

重載流運營商最好的辦法是保持他們作爲朋友。

在你的PP類聲明:

friend istream& operator >>(istream& in, pp& obj); 

在你的CPP文件:

istream& operator>>(istream &in, pp& obj) 
{ 
    // Your code 
} 
+0

如果你不需要他們訪問私人領域,那麼不需要讓他們成爲朋友。 –

+0

嗨那裏我不能使用朋友,因爲我有4個其他類,我不得不重載相同的運算符與不同的功能! :d – mister

+0

你可以用不同的對象多次超載的全球運營商>>:istream的&運算符>>(istream的&中,PP1 & obj);的IStream和操作>>(istream的&中,PP2 & obj); 所以這不應該是一個問題 – Sam