operator<< overloading

How do you overload << so that when you want to print details in a class you can simply do this:

int main()
{
testClass ob(123,123);
cout<<ob;
}

class testClass
{
testClass(int x,int y)
{
xx =x;
yy = y;
}

ostream& testClass::operator<<(ostream& out)
{
out<<"hi";
return out;
}//end of operator<< method
};

this is what i did but did not work.
Last edited on
1
2
3
4
void operator<< () {
       cout << this->some_member << endl;
       return;
}


opeartor is a function just like any other,
it has return type, arguments and body where function identifer is "operator" with operator added like "operator++"

operator inside a class is defined for this object.
operator outside the class is defined for any operands.
Last edited on
hey thanks for the reply, unfortunately i followed your template and got the following error:

main.cpp: In function ‘int main()’:
main.cpp:47: error: no match for ‘operator<<’ in ‘std::cout << pd’
/usr/include/c++/4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
hi,
can you please paste your full code with operator added?

please use code tags to format your code
Last edited on
header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class testClass
{
	public:
		//constructor
		testClass();
		testClass(int, int);
		testClass(const testClass &pd);
		//operator overloaded methods
		virtual testClass operator-(testClass pd);
		virtual bool operator<(testClass pd) const;
		virtual bool operator>(testClass pd) const;
		virtual bool operator==(testClass pd);
		void operator<<(testClass& pd);
}


.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "testClass.h"

//default constructor
testClass::testClass()
{
	x = 0;
	y = 0;
	setDistFrOrigin();
}//end of default constructor

//constructor
testClass::testClass(int x,int y)
{
	this->x = x;
	this->y = y;
	setDistFrOrigin();
}//end of constructor

//copy constructor
testClass::testClass(const testClass &pd)
{
	this->x = pd.x;
	this->y = pd.y;
	this->distFrOrigin = pd.distFrOrigin;
}//end of constructor

void testClass::operator<<(testClass& pd)
{
	cout<<"hi";
}//end of operator<< method 


main

1
2
3
4
5
6
int main()
{
	testClass pd(123,123);
	cout << pd;
	//mainMenu();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class testClass
{
friend ostream& operator<<(ostream& pd, testClass& obj);
public:
	int x,y;  //MEMBERS ADDED!
	//...
}; //SEMICOLON ADDED!
ostream& operator<<(ostream& pd, testClass& obj)
{
	pd << obj.x << " " << obj.y << endl;
	return pd;
}
int main() {
        testClass pd(123,123);
	cout << pd;
        return 0;
}


you are missing members in your class ( x, y) ?
and loads of other things like semicolon after class definition.
Last edited on
Oh i only pasted partial of the class due to a huge program i have. Anyway is it mandatory to friend it?
I need to overload the << operator for 4 diffrent classes. And the algorithm of output will be diffrent
hi again dupdupdup,

yes you can achive that in many ways!

one of the ways is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class testClass
{
public:
	int x,y;
	testClass(int a, int b) : x(a), y(b) {}
	ostream & operator<<(ostream& o) {
		return o << this->x << "  " << this->y;
	}
};

int main()  {
	testClass pd(123,123);
        pd.operator<<(cout);
	cout << endl << "finish...";
	cin.ignore();
	return 0;
}


funny way No. 2:

1
2
3
4
5
6
7
int main()  {
	testClass pd(123,123);
        pd << cout;
	cout << endl << "finish...";
	cin.ignore();
	return 0;
}


it's kinda diplomatic which way will you choose :D

cheers!
Last edited on
oh thanks alot that works fantastic.
by the way, i have a few question on your reply.

Before i print the class do i have to do this all the time?
pd.operator<<(cout);

also, is cin.ignore() mandatory? if so why?

thank you codekiddy for your kind help so far! :D
Before i print the class do i have to do this all the time?
pd.operator<<(cout);

No you don't.
I've edited my previous post with another "way", so you probably didn't se that.

also, is cin.ignore() mandatory? if so why?

it's not mandatory but it's recomended because system("pause") is for windows only, thus your code becomes unportable to other platforms.

while cin.ignore(); is C++ standard and works on any platform.

also system("pause") sims to have some security wholes but I don't know much about that.
also when you use system("pause") then others think that you're noob, but I wouldn't say that.

I'm glad to be able helping you.
cheers mate!
Last edited on
Topic archived. No new replies allowed.