简体   繁体   English

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

[英]C++ Unary - Operator Overload Won't Compile

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? 任何人都可以建议我在这里需要做什么吗?

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- 或者操作数不能是临时值,例如operator-的返回值 -

And unary minus should rather be: 而且一元减去应该是:

frag operator - () const;

since it shouldn't modify the operand. 因为它不应该修改操作数。

You don't have an operator+ that can operate on temporaries. 你没有可以在临时工作的operator+ Temporaries cannot be passed as non-const reference. Temporaries不能作为非const引用传递。

Change the signature of your operator+ to: operator+的签名更改为:

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

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. 我猜你想要这两个函数来实现你在frag声明的成员函数 - 但它们没有。 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); 另一方面,这些也没有任何意义 - 特别是作为成员函数,它们通常被调用为: 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: 它们实际上都像全局函数一样编写 - 作为成员函数,它们通常只接受一个参数,并将this用作第一个参数:

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".... 如果是这样,请随意忽略这整个“答案”....

已经给出了答案(const参数),但是我想提一下,Visual C ++ 9(VS-2008)确实会在没有警告的情况下编译上面的内容。

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

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