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
#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
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.
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--()
,
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--()
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.