Click to See Complete Forum and Search --> : What's wrong with the code?


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

Mike Nashadka
05-22-2000, 09:15 PM
There are several issues here. The biggest one is the fact that the signature
in your declaration
and definition are different. There are some missing reference operators
(&) on your istream and
ostream objects in your definition. Also, many compilers have problems with
the
"using namespace std;" when it comes to resolving friend signatures. There
are two ways around this:

1. Use the fully qualified std name for your functions:
class Complexo
{
public:
friend std::ostream& operator<<(std::ostream& os, const Complexo& aComplexo);
};

inline std::ostream& operator<<( std::ostream& os, const Complexo& aComplexo
)
{
return os << aComplexo.real << "+i" << aComplexo.imaginario << std::endl;
}

2. Put the definition of the function in the class declaration:
class Complexo
{
public:
friend ostream& operator<<(ostream& os, const Complexo& aComplexo)
{
return os << aComplexo.real << "+i" << aComplexo.imaginario
<< endl;
}
};

This will probably be acceptable since you are already inlining the function.

"Luis Abreu" <luisabreu@netmadeira.com> wrote:
>
>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
>

nomorsht
05-22-2000, 09:15 PM
"Luis Abreu" <luisabreu@netmadeira.com> wrote:
>
>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;
>}
>

*************************************************************

You forgot to add the referencing operator (&) to function below
>inline ostream operator<<( ostream os, const Complexo& aComplexo )
****************************************************************
Should be:
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
>
The function prototype has to be identical in the declaration and the definition.


example:

void thisfunction(int &);<-----This function is not the same as
void thisfunction(int n) <-----this function.
{
code...
};

Luis Abreu
05-23-2000, 04:59 AM
Thanks!

Luis Abreu

Swap
06-01-2000, 07:31 AM
I think that you would write operator function as global function with two
parameters. I have similar problem and I solve it this way. You can post
the e-mail to my address > formaldehyd@post.cz.
Swap