Luis Abreu
05-22-2000, 06:26 PM
Could anyone tell me what's wrong with this code? Well, when I compile I get
some unexpected errors (which are signaled in the code...). I'm using VC++.
The errors that I get are the following:
1. in overloading operators >> and << (which are declared as friends), I
get an error message which says that I can't access the private member of
the class (but the function is declared as friend of the class, shouldn't
it get acces to its private member???).
2. the second type of error is in main, where I use the class. Here I get
an error relationated with operator <<, which says that << is ambiguous (but
I've declared it as friend of class Complexo and overloaded, how can this
be???).
I have built class Complexo which is separated in 2 files:
--->Complexo.h
#include <iostream>
using namespace std;
class Complexo
{
public:
//CONSTRUTORES
Complexo();
Complexo( int aReal, int aImaginario );
Complexo( int aReal );
//CONSTRUTOR COPIA
Complexo( const Complexo& aComplexo );
//METODOS
int GetReal() const { return real;}
int GetImaginario() const { return imaginario;}
bool operator==( const Complexo& aComplexo );
Complexo& operator=( const Complexo& aComplexo );
//FRIENDS
friend ostream& operator<<(ostream& os, const Complexo& aComplexo );
friend istream& operator>>(istream& is, Complexo& aComplexo );
//DESTRUTOR
virtual ~Complexo();
private:
int real;
int imaginario;
};
inline bool Complexo::operator==( const Complexo& aComplexo )
{
return (real == aComplexo.real && imaginario == aComplexo.imaginario) ?
true : false;
}
inline ostream operator<<( ostream os, const Complexo& aComplexo )
{
return os << aComplexo.real << "+i" << aComplexo.imaginario << endl; //
GET ERROR HERE (can't access private member of the class????)
}
inline istream operator>>( istream is, Complexo& aComplexo )
{
int a, b;
is >> a >> b;
aComplexo = Complexo(a,b);
return is;
}
--->COMPLEXO.CPP
#include "Complexo.h"
Complexo::Complexo(): real( 0 ), imaginario( 0 )
{
}
Complexo::Complexo(int aReal, int aImaginario ): real( aReal ), imaginario(
aImaginario )
{
}
Complexo::Complexo(int aReal): real( aReal ), imaginario( 0 )
{
}
Complexo::Complexo( const Complexo& aComplexo )
{
real = aComplexo.real;
imaginario = aComplexo.imaginario;
}
Complexo::~Complexo()
{
}
--->MAIN.CPP
And the main file is:
#include "Complexo.h"
using namespace std;
main()
{
Complexo a,b(1),c(2,3),d(c);
cout <<b; //GET ERROR HERE(operator << is ambiguous???
return 0;
}
I know that I'm doing something wrong, but I just can't figure it out. And
to think that when I started, I thought that this would be easy...
Thanks in advance
Luis Abreu
some unexpected errors (which are signaled in the code...). I'm using VC++.
The errors that I get are the following:
1. in overloading operators >> and << (which are declared as friends), I
get an error message which says that I can't access the private member of
the class (but the function is declared as friend of the class, shouldn't
it get acces to its private member???).
2. the second type of error is in main, where I use the class. Here I get
an error relationated with operator <<, which says that << is ambiguous (but
I've declared it as friend of class Complexo and overloaded, how can this
be???).
I have built class Complexo which is separated in 2 files:
--->Complexo.h
#include <iostream>
using namespace std;
class Complexo
{
public:
//CONSTRUTORES
Complexo();
Complexo( int aReal, int aImaginario );
Complexo( int aReal );
//CONSTRUTOR COPIA
Complexo( const Complexo& aComplexo );
//METODOS
int GetReal() const { return real;}
int GetImaginario() const { return imaginario;}
bool operator==( const Complexo& aComplexo );
Complexo& operator=( const Complexo& aComplexo );
//FRIENDS
friend ostream& operator<<(ostream& os, const Complexo& aComplexo );
friend istream& operator>>(istream& is, Complexo& aComplexo );
//DESTRUTOR
virtual ~Complexo();
private:
int real;
int imaginario;
};
inline bool Complexo::operator==( const Complexo& aComplexo )
{
return (real == aComplexo.real && imaginario == aComplexo.imaginario) ?
true : false;
}
inline ostream operator<<( ostream os, const Complexo& aComplexo )
{
return os << aComplexo.real << "+i" << aComplexo.imaginario << endl; //
GET ERROR HERE (can't access private member of the class????)
}
inline istream operator>>( istream is, Complexo& aComplexo )
{
int a, b;
is >> a >> b;
aComplexo = Complexo(a,b);
return is;
}
--->COMPLEXO.CPP
#include "Complexo.h"
Complexo::Complexo(): real( 0 ), imaginario( 0 )
{
}
Complexo::Complexo(int aReal, int aImaginario ): real( aReal ), imaginario(
aImaginario )
{
}
Complexo::Complexo(int aReal): real( aReal ), imaginario( 0 )
{
}
Complexo::Complexo( const Complexo& aComplexo )
{
real = aComplexo.real;
imaginario = aComplexo.imaginario;
}
Complexo::~Complexo()
{
}
--->MAIN.CPP
And the main file is:
#include "Complexo.h"
using namespace std;
main()
{
Complexo a,b(1),c(2,3),d(c);
cout <<b; //GET ERROR HERE(operator << is ambiguous???
return 0;
}
I know that I'm doing something wrong, but I just can't figure it out. And
to think that when I started, I thought that this would be easy...
Thanks in advance
Luis Abreu