简体   繁体   English

运算符重载-(前缀)

[英]Overload of operator— (prefix)

I tried overload operator-- prefix, but I have errors, Someone for help? 我尝试了重载运算符-前缀,但是出现错误,有人需要帮助吗?

#include <iostream>
#include "Circulo.h"

using namespace std;

int main()
{
    //parametrizado
    Circulo c1(10.5, 20.0, 1, "Rojo Intenso"),c2, c3;
/*
    c2 = c1--;    // decrementa, si se puede, el diámetro

    c3 = c2++;    // incrementa, si se puede, el diámetro

    Circulo c4 = c3++;

    c4.CambiarColor("Amarillo patito flúo");


    cout << c1 << c2 << c3 << (c4 = c3) << endl;
*/
    return 0;
}

Circulo.cpp Circulo.cpp

#include "Circulo.h"
#include <string.h>

using namespace std;

Circulo :: Circulo() //por defecto
{
    x=5.0;
    y=6.0;
    diam=50;
    strcpy(Color,"Rojo");
}

Circulo::Circulo(float x,float y,unsigned diam,char *Color) //parametrizado
{
    this->x=x;
    this->y=y;
    this->diam=diam;
    this->Color= Color;
}

Circulo::Circulo(const Circulo& obj) //por copia
{
    this->x=obj.x;
    this->y=obj.y;
    this->diam=diam;
    this->Color=Color;
}

Circulo :: operator--(const Circulo &obj)const
{
    Circulo aux(this->real+obj.x);
    return aux;
}

Circulo.h 循环

#ifndef CIRCULO_H
#define CIRCULO_H
#include <iostream>

using namespace std;

class Circulo
{
    public:
        Circulo();
        Circulo(float x,float y,unsigned diam,char *Color);
        Circulo(const Circulo& obj);
        Circulo operator --(const Circulo &obj) const;
    private:
        float x,y;
        unsigned diam;
        char *Color;

};

#endif // CIRCULO_H

error: postfix 'Circulo Circulo::operator--(const Circulo&) const' must take 'int' as its argument 错误:后缀'Circulo Circulo :: operator-(const Circulo&)const'必须采用'int'作为其参数

C:\Documents and Settings\laboratorios\Mis documentos\Clase Circulo\Circulo.h|13|error: postfix 'Circulo Circulo::operator--(const Circulo&) const' must take 'int' as its argument|
C:\Documents and Settings\laboratorios\Mis documentos\Clase Circulo\Circulo.cpp|30|error: ISO C++ forbids declaration of 'operator--' with no type [-fpermissive]|
C:\Documents and Settings\laboratorios\Mis documentos\Clase Circulo\Circulo.cpp|30|error: postfix 'int Circulo::operator--(const Circulo&) const' must take 'int' as its argument|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|

When you overload an operator that is a member function (and not a friend ), you don't need to specify its parameter. 当重载属于成员函数(而不是friend )的运算符时,无需指定其参数。

Circulo operator --(const Circulo &obj) const;

should be 应该

Circulo operator --(); // probably not const, since you modify the instance

The error tells you that the compiler expects a (dummy) int , which is the convention used by the compiler to realize that you wanted to define the postfix operator--() , 该错误告诉您,编译器期望一个(dummy) int ,这是编译器用来意识到您想要定义后缀operator--()的约定。

Circulo operator --(int); // dummy int, convention postfix

See Operator overloading (Unary arithmetic operators) for one of the best guides on operator overloading. 有关运算符重载的最佳指南之一,请参见运算符重载 (一元算术运算符)。

You invoke these operators on the current instance, like 您可以在当前实例上调用这些运算符,例如

Circulo c;
--c; // same as c.operator--();

so there is no need to specify a parameter. 因此,无需指定参数。

Also, your implementation is incorrect (thanks @Praetorian), you don't need to make a copy of the object, but modify the instance directly, then return a reference to *this (for prefix operator--() ). 同样,您的实现是不正确的(感谢@Praetorian),您不需要复制对象,而是直接修改实例,然后返回对*this的引用(对于前缀operator--() )。

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

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