C++一元 - 运算符重载无法编译

发布于 2024-08-30 23:46:09 字数 1124 浏览 2 评论 0原文

我正在尝试创建一个重载的一元 - 运算符,但无法编译代码。代码的精简版本如下:-

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆凄美了谁 2024-09-06 23:46:09

常量正确性

这必须是

 frag operator+ (const frag &oper1, const frag &oper2);

,否则操作数不能是临时的,例如 operator- 的返回值,

而一元减号应该是:

frag operator - () const;

因为它应该' t 修改操作数。

const-correctness

This has to be

 frag operator+ (const frag &oper1, const frag &oper2);

or else the operands can't be temporaries, such as the return value of operator-

And unary minus should rather be:

frag operator - () const;

since it shouldn't modify the operand.

油饼 2024-09-06 23:46:09

您没有可以对临时对象进行操作的 operator+。临时对象不能作为非常量引用传递。

operator+ 的签名更改为:

frag operator + (const frag &oper1, const frag &oper2);

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 operator + (const frag &oper1, const frag &oper2);
别理我 2024-09-06 23:46:09

尽管您的问题已经得到了相当好的回答,但我认为值得提及关于您的代码的另一点。现在,您有以下声明:

class frag
{
    public:

        frag myfunc  (frag oper1,
                      frag oper2);
        frag myfunc2  (frag oper1,
                      frag oper2);

...并且您有以下函数:

frag myfunc (frag oper1, frag oper2)
{
    return oper1 + -oper2;
}

frag myfunc2 (frag oper1, frag oper2)
{
    return oper1 + oper2;
}

我猜您打算用这两个函数来实现您在 frag 中声明的成员函数 - 但它们没有't。相反,您有两个由 never Defined 声明的成员函数,并且这两个函数是碰巧具有相似名称的全局函数。为了使它们成为您声明的成员函数,您需要将声明更改为以下内容:

frag frag::myfunc(frag oper1, frag oper2) { 
    return oper1 + -oper2;
}

frag frag::myfunc2(frag oper1, frag oper2) { 
    return oper1 + oper2;
}

另一方面,这些方式也没有任何意义 - 特别是作为成员函数,它们通常会被调用为:a.myfunc(b,c); 不过,它们实际上都是像全局函数一样编写的——作为成员函数,它们通常只需要一个参数,并使用 this 作为第一个参数:

frag frag::myfunc1(frag oper) { 
    return *this + -oper;
}
frag frag::myfunc2(frag oper) { 
    return *this + oper;
}

当然,这可能只是尝试将原始代码减少到发布所需的最低限度而产生的意外副作用。如果是这样,请随意忽略整个“答案”......

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:

class frag
{
    public:

        frag myfunc  (frag oper1,
                      frag oper2);
        frag myfunc2  (frag oper1,
                      frag oper2);

... and you have the following functions:

frag myfunc (frag oper1, frag oper2)
{
    return oper1 + -oper2;
}

frag myfunc2 (frag oper1, frag oper2)
{
    return oper1 + oper2;
}

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:

frag frag::myfunc(frag oper1, frag oper2) { 
    return oper1 + -oper2;
}

frag frag::myfunc2(frag oper1, frag oper2) { 
    return oper1 + oper2;
}

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 use this as the first parameter:

frag frag::myfunc1(frag oper) { 
    return *this + -oper;
}
frag frag::myfunc2(frag oper) { 
    return *this + oper;
}

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"....

空城旧梦 2024-09-06 23:46:09

答案已经给出(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文