简体   繁体   English

运算符重载代码编译错误,模板参数推导/替换失败

[英]Operator overloading code compilation error, template argument deduction/substitution failure

I'm trying to apply some concepts of operator loading that I've learnt in the following C++ class. 我正在尝试应用以下C ++类中学习的一些运算符加载概念。

#include<iostream>
using namespace std;

class Point
{
private:
    int x, y;
public:
    Point(int, int);
    Point operator+(const Point&);
    friend istream& operator>>(istream& in, Point&);
    friend ostream& operator<<(ostream& out, Point&);
    Point operator=(const Point&);
};

Point::Point(int x = 0, int y = 0)
    :x(x), y(y){}

Point Point::operator+(const Point& p)
{
    int r1, r2;
    r1 = x + p.x;
    r2 = y + p.y;
    return Point(r1, r2);
}

Point Point::operator=(const Point& p)
{
    this->x = p.x;
    this->y = p.y;
    return *this;
}

istream& operator>>(istream& in, Point& p)
{
    char openParenthesis, closeParenthesis, comma;
    cout << "Enter data in the format (1,2): ";
    in >> openParenthesis >> p.x >> comma >> p.y >> closeParenthesis;
    return in;
}

ostream& operator<<(ostream& out, Point& p)
{
    out << "(" << p.x << "," << p.y << ")";
    return out;
}

int main()
{
    Point a, b;
    cin >> a >> b;

    cout << "a + b is: " << a + b << endl;
    return 0;
}

The code compiles and runs fine on Visual Studio. 该代码可以在Visual Studio上编译并正常运行。 But when I try to compile it on Linux with gcc, it throws a long list of errors along the lines of: 但是,当我尝试在Linux上使用gcc对其进行编译时,它会抛出一系列错误:

In file included from /usr/include/c++/4.8/iostream:39:0, from optr_overload.cpp:1: /usr/include/c++/4.8/ostream:471:5: note: template std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT) operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c) ^ /usr/include/c++/4.8/ostream:471:5: note: template argument deduction/substitution failed: optr_overload.cpp:53:30: note: 在/usr/include/c++/4.8/iostream:39:0中包含的文件中,从optr_overload.cpp:1:/usr/include/c++/4.8/ostream:471:5:注意:模板std :: basic_ostream <_CharT ,_Traits>&std :: operator <<(std :: basic_ostream <_CharT,_Traits>&,_CharT)运算符<<(basic_ostream <_CharT,_Traits>&__out,_CharT __c)^ /usr/include/c++/4.8/ ostream:471:5:注意:模板参数推导/替换失败:optr_overload.cpp:53:30:注意:
deduced conflicting types for parameter '_CharT' ('char' and 'Point') cout << "a + b is: " << a + b << endl; 推导参数'_CharT'('char'和'Point')的冲突类型cout <<“ a + b是:” << a + b << endl;

I understand that the problem lies with the line where I passed "a + b" to the overloaded binary stream insertion operator which only receives reference to one Point object as the argument. 我知道问题出在我将“ a + b”传递给重载的二进制流插入运算符的那行,该运算符仅接收对一个Point对象的引用作为参数。 But I've no idea how to fix the code other than assigning "a + b" to a third object and pass that single object as the argument to "<<". 但是除了将“ a + b”分配给第三个对象并将该单个对象作为参数传递给“ <<”之外,我不知道如何修复代码。 Could someone explain to me what exactly needs to be done in order for gcc to compile my code, preferably without involving the use of an extra placeholder object. 有人可以向我解释为了使gcc编译我的代码需要做什么,最好不涉及使用额外的占位符对象。

The value computed with a + b is a temporary object and therefore cannot be passed as a Point& to operator<< ; a + b计算的值是一个临时对象,因此不能作为Point&传递给operator<< ; the language only allows temporaries to be passed as const Point& instead. 该语言仅允许将临时变量作为const Point&传递。 Just change he declaration of the output operator to accept a const Point& . 只需更改输出运算符的声明即可接受const Point&

Allowing passing a temporary result as a non-const reference was a known bug in old versions of VC++. 在旧版本的VC ++中,允许将临时结果作为非常量引用传递是已知的错误。

You've got almost everything right, but your ostream& operator<<() should take Point by a const reference: 您几乎一切都正确,但是您的ostream& operator<<()应该通过const引用获取Point:

friend ostream& operator<<(ostream& out, const Point&);

This should fix your issue. 这应该可以解决您的问题。

(And don't worry about Point::x and Point::y being private, you've already declared your operator<< as a friend.) (并且不必担心Point::xPoint::y是私有的,您已经声明了operator<<为朋友。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM