C++一元 - 运算符重载无法编译
我正在尝试创建一个重载的一元 - 运算符,但无法编译代码。代码的精简版本如下:-
class frag
{
public:
frag myfunc (frag oper1,
frag oper2);
frag myfunc2 (frag oper1,
frag oper2);
friend frag operator + (frag &oper1,
frag &oper2);
frag operator - ()
{
frag f;
f.element = -element;
return f;
}
private:
int element;
};
frag myfunc (frag oper1, frag oper2)
{
return oper1 + -oper2;
}
frag myfunc2 (frag oper1, frag oper2)
{
return oper1 + oper2;
}
frag operator+ (frag &oper1, frag &oper2)
{
frag innerfrag;
innerfrag.element = oper1.element + oper2.element;
return innerfrag;
}
编译器报告...
/home/brian/Desktop/frag.hpp: In function ‘frag myfunc(frag, frag)’:
/home/brian/Desktop/frag.hpp:41: error: no match for ‘operator+’ in ‘oper1 + oper2.frag::operator-()’
/home/brian/Desktop/frag.hpp:16: note: candidates are: frag operator+(frag&, frag&)
有人可以建议我在这里需要做什么吗?
I am attempting to create an overloaded unary - operator but can't get the code to compile. A cut-down version of the code is as follows:-
class frag
{
public:
frag myfunc (frag oper1,
frag oper2);
frag myfunc2 (frag oper1,
frag oper2);
friend frag operator + (frag &oper1,
frag &oper2);
frag operator - ()
{
frag f;
f.element = -element;
return f;
}
private:
int element;
};
frag myfunc (frag oper1, frag oper2)
{
return oper1 + -oper2;
}
frag myfunc2 (frag oper1, frag oper2)
{
return oper1 + oper2;
}
frag operator+ (frag &oper1, frag &oper2)
{
frag innerfrag;
innerfrag.element = oper1.element + oper2.element;
return innerfrag;
}
The compiler reports...
/home/brian/Desktop/frag.hpp: In function ‘frag myfunc(frag, frag)’:
/home/brian/Desktop/frag.hpp:41: error: no match for ‘operator+’ in ‘oper1 + oper2.frag::operator-()’
/home/brian/Desktop/frag.hpp:16: note: candidates are: frag operator+(frag&, frag&)
Could anyone suggest what I need to be doing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
常量正确性
这必须是
,否则操作数不能是临时的,例如
operator-
的返回值,而一元减号应该是:
因为它应该' t 修改操作数。
const-correctness
This has to be
or else the operands can't be temporaries, such as the return value of
operator-
And unary minus should rather be:
since it shouldn't modify the operand.
您没有可以对临时对象进行操作的
operator+
。临时对象不能作为非常量引用传递。将
operator+
的签名更改为:You don't have an
operator+
that can operate on temporaries. Temporaries cannot be passed as non-const reference.Change the signature of your
operator+
to:尽管您的问题已经得到了相当好的回答,但我认为值得提及关于您的代码的另一点。现在,您有以下声明:
...并且您有以下函数:
我猜您打算用这两个函数来实现您在
frag
中声明的成员函数 - 但它们没有't。相反,您有两个由 never Defined 声明的成员函数,并且这两个函数是碰巧具有相似名称的全局函数。为了使它们成为您声明的成员函数,您需要将声明更改为以下内容:另一方面,这些方式也没有任何意义 - 特别是作为成员函数,它们通常会被调用为:
a.myfunc(b,c);
不过,它们实际上都是像全局函数一样编写的——作为成员函数,它们通常只需要一个参数,并使用this
作为第一个参数:当然,这可能只是尝试将原始代码减少到发布所需的最低限度而产生的意外副作用。如果是这样,请随意忽略整个“答案”......
Though your question has already been answered reasonably well, I think it's worth mentioning another point about your code. Right now, you have the following declarations:
... and you have the following functions:
I'd guess that you intended these two functions to implement the member functions you declared in
frag
-- but they don't. Instead, you have two member functions that are declared by never defined, and these two are global functions that happen to have similar names. For them to be the member functions you declared, you need to change the declaration to something like:On the other hand, these don't really make any sense that way either -- in particular, as member functions, they'll normally be invoked as something like:
a.myfunc(b,c);
They're both really written like global functions though -- as member functions, they'd normally take only one parameter, and usethis
as the first parameter:Of course, this may just be an accidental side effect from trying to reduce the original code to a minimum necessary for posting. If so, please feel free to disregard this whole "answer"....
答案已经给出(const 参数),但我想提一下,Visual C++ 9 (VS-2008) 确实会在没有警告的情况下编译上述内容。
The answer has already been given (const arguments), but I'd like to mention that Visual C++ 9 (VS-2008) would indeed compile the above without warning.