从算术运算符返回时,为什么我不能将此类作为引用传递?

Neo*_*ana 0 c++ operator-overloading pass-by-reference c++11

如果我有一个像这样的简单类:

template<typename T>
class coord
{
public:

    coord() : x(0), y(0)
    {
    }

    coord(T X, T Y) : x(X), y(Y)
    {
    }

    T x;
    T y;

    coord& operator-=(const coord& rhs)
    {
        (*this).x -= rhs.x;
        (*this).y -= rhs.y;
        return *this;
    }

    coord& operator+=(const coord& rhs)
    {
        (*this).x += rhs.x;
        (*this).y += rhs.y;
        return *this;
    }
};
Run Code Online (Sandbox Code Playgroud)

与以下运营商一起(他们不是friend因为没有私人会员可以访问).

template<typename T = int>
inline coord<T> operator-(coord<T> lhs, const coord<T>& rhs)
{
    lhs -= rhs;
    return lhs;
}

template<typename T = int>
inline coord<T> operator+(coord<T> lhs, const coord<T>& rhs)
{
    lhs += rhs;
    return lhs;
}
Run Code Online (Sandbox Code Playgroud)

在我的代码的其他地方,我有另一个类A,其方法如下所示:

void A::SetVarC(coord<int>& c)
{
    m_c = c;
}
Run Code Online (Sandbox Code Playgroud)

(假设还有一个吸气剂m_c)

当我尝试使用加法和减法运算符调用此方法时,我重载:

int x = 1;
int y = 1;

A* a = new A();

coord c1(1,2);

a->SetVarC(c1 - a->GetVarC() + coord<int>(x,y));
Run Code Online (Sandbox Code Playgroud)

我得到的有来自没有已知的转换的错误coord<int>coord<int>&.我可以看到我的减法和加法运算符没有返回引用,但我认为这无关紧要.我正在使用C++ 11 ...移动语义在这里发挥作用?

Jar*_*d42 5

临时不能绑定到非const引用,更改SetVarC

void A::SetVarC(const coord<int>& c)
{
    m_c = c;
}
Run Code Online (Sandbox Code Playgroud)

要么

void A::SetVarC(coord<int> c)
{
    m_c = std::move(c);
}
Run Code Online (Sandbox Code Playgroud)