Computer Science Canada Overloading Operator<< |
Author: | The_$hit [ Wed Apr 27, 2005 8:48 pm ] | ||
Post subject: | Overloading Operator<< | ||
Does any one know how to overload the operator << or >> to use it for std::cout or std::cin? I got an understanding of how to do it but my compilier wont let me have 2 parameters when i overload operators. right now it looks like this:
|
Author: | wtd [ Wed Apr 27, 2005 9:01 pm ] | ||
Post subject: | |||
This really should be in C/C++ Help, but here's a simple example. You need to use a friend function.
|
Author: | The_$hit [ Thu Apr 28, 2005 10:26 am ] |
Post subject: | |
CodeWarrior 8 won't let me overload this operator with two parameters. that is why I had to use the *this->. there are no syntax errors but it would let me cout<< the object. |
Author: | wtd [ Thu Apr 28, 2005 11:53 am ] |
Post subject: | |
Did you try exactly the code I gave you? Can you give us the exact contents of the error message? |
Author: | The_$hit [ Thu Apr 28, 2005 4:25 pm ] |
Post subject: | |
Thank you it works. I was missing the friend, it was giving me the message "illigal 'operator' decleration". when i added friend the message stopped. had to be friend function to do that. |
Author: | wtd [ Thu Apr 28, 2005 4:34 pm ] |
Post subject: | |
Yes, a friend function is basically one which lives outside of the class. In this case, the insertion ( << ) and extraction ( >> ) operators are not members of the class. However they are friends, and this is important because that status allows them to access private and protected members of the class. |
Author: | The_$hit [ Thu Apr 28, 2005 4:59 pm ] | ||
Post subject: | |||
ooo ok. So how would you make it so that you can cin an object is this right
|
Author: | wtd [ Thu Apr 28, 2005 5:25 pm ] | ||||||
Post subject: | |||||||
First off, let's clean this up.
Now, let's look at what's wrong here. First, you only use the friend when declaring the function within the class. In this case your String class. Next, you're using the sd::ostream class. We're not outputting, so we need to use the std::istream class. Also, you're referring to cin, even though you have a stream variable to work with passed as an argument to the function.
Now, to go any further, I need to see your String class. Let me guess though, it looks looks a bit like:
A final note for this post: why are you doing this? Standard C++ provides a perfectly good string class in std::string. |
Author: | The_$hit [ Thu Apr 28, 2005 9:10 pm ] | ||
Post subject: | |||
I am doing this class so i can customize it and i know all of the functions in it. It is also for good for practice with overloading operators, arrays, i was also thinking that it would help me understand pointers.
|
Author: | wtd [ Thu Apr 28, 2005 10:01 pm ] | ||
Post subject: | |||
You'll find help far more forthcoming if you post well-formatted code.
|
Author: | The_$hit [ Fri Apr 29, 2005 9:04 pm ] |
Post subject: | |
This program does not let me cin the object. there are no syntax errors in theclass but when you cin << a String it returns this error: illegal operands 'std::basic_istream<char,std::char_traits<chars>>' >> 'String' |
Author: | wtd [ Fri Apr 29, 2005 9:57 pm ] | ||||
Post subject: | |||||
Ok, let's look at a very simple example of overloading the extraction ( >> ) operator.
So, what am I doing here? Well, I have a class "foo" with a private instance variable "a" which is an int. I have a public method "get_a()" to retrieve that value. I also have a friend, which is the extaction operator. The implementation of get_a() is so simple it warrants no further mention. Now, when I implement the extraction operator, it isn't a member of the foo class. Rather it takes the input stream and a reference to a foo object. The operator reads into that object's "a" instance variable, and then the input stream is returned, allowing for chaining them together, like so:
Any questions? |
Author: | The_$hit [ Sat Apr 30, 2005 12:15 am ] | ||
Post subject: | |||
Thank you very much for all of your help. My completed String class is here:
**** Edit hmmmmm.... soething is wrong? My console debug quits automatically |
Author: | wtd [ Sat Apr 30, 2005 12:52 am ] | ||||||||||||||||||
Post subject: | |||||||||||||||||||
Tips:
|