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 ( );
};
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()