operator overloading

hi,
I have written a program for operator over loading ad below and i am getting an error as
39 C:\Dev-Cpp\complex.cpp no match for 'operator<<' in 'std::cout << (&C1)->complex::display()'

what might be the problem in my code, please help me..

#include <cstdlib>
#include <iostream>

using namespace std;

class complex {

double real;
double imag;
public :
complex ( ) { }
complex ( double a, double b ) {
real = a;
imag = b;
}
complex operator+ ( complex ) ;
void display ( );
};


complex complex :: operator+ ( complex c ) {
complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
void complex :: display ( void ) {
std::cout << real;
std::cout << imag;
}
int main(int argc, char *argv[])
{
complex C1, C2, C3;

C1 = complex ( 2.5, 3.5);
C2 = complex ( 3.5, 2.5 );
C3 = C1 + C2;

std::cout << "C1 = " << C1.display ( ) << endl;
std::cout << " C2 = " << C2.display ( ) << endl;
std::cout << " C3 = " <<C3.display ( ) << endl;

system("PAUSE");
return EXIT_SUCCESS;

}
Last edited on
i got ma mistake and corrected ,
i need to call C1.display, instead of cout << C1.display ( );

thank myself
i got ma mistake and corrected ,
i need to call C1.display, instead of cout << C1.display ( );

thank myself
Nope. Then all you display is the pointer to that function. The way you wrote the 'display()' function you should write something like that std::cout << "C1 = "; C1.display ( ); std::cout << endl;

You can overload the opterator<<() like so:

1
2
3
4
5
ostream &opterator<<(ostream &os, const complex &cmlx) // golbal function
{
  os << cmlx.GetReal() << " " << cmlx.GetImag(); // Note that you need to add the getters GetReal() and GetImag() to you  class complex
  return os;
}


Then you cann indeed write std::cout << "C1 = " << C1 << endl; // Note no display()
ya, but here i am trying to overload operator "+", and i am getting the expected result,
i had called
C1.display ( );
C2.display ( );
C3.display ( );

and display ( ) is going to print the values for me.

Thank you.
Sagar
Topic archived. No new replies allowed.