2011-11-21 66 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