Oh, and if your overloading a << operator for a class with cout (or any ostream object) on the left, you'll want to return the ostream object, not this. That will let you chain more commands in a row.
In your class you'd declare: friend ostream& operator<<(ostream& o, const MyClass& c);
Then outside of the class you'd define:
1 2 3 4 5
ostream& operator<<(ostream& o, const MyClass& c)
{
o << c.member1 << c.member2;
return o;
}
Here's another example of a class that simultaneously writes to a file and console. This one uses the << overloading, has the custom class on the left, and uses *this:
this is a pointer to the object. *this is the object itself. You weren't wrong here.
If you have
1 2 3 4
class MyClass
{
// ...
};
Then you do this:
1 2
MyClass a;
cout << a;
You will have problems because the cout has no idea what to do with this class. A class doesn't really have a value, it could contain hundreds of different containers or maybe none! Knowing which ones to output is not cout's job.
Doing: cout << *this is exactly the same thing. If you haven't manually defined the behavior of sticking your class into a cout, then you're not going to be able to compile. To define the behavior of sticking your class into a cout see this: http://cplusplus.com/forum/beginner/64156/#msg347038
tl;dr
Your problem is not with *this, it's that you haven't overloaded the << operator to accept ostream<<YourClass.
oh yeah... right... thanks stew... i totally didn't notice that part... thanks again man... i forgot that we cout a class, that's why the cout cannot determine which value of a class should be cout-ed...