![]() |
Overloading operator== for new class
I've created a new class Item, and I am attempting to make comparisons with it. To do this, I've found that I need to overload operator==. The code below is wrong, but since I've never overloaded == before, I'm not sure why it doesn't work. Here's the listing, and the errors I am getting:
:
And, the errors: 1. 'numbers' is not a type (line 4 of this block) 2. request for member of non-aggregate type before '.' token. (line 4) 3. 'numbers' is not a type (line 6) 4. request for member of non-aggregate type before '.' token. (line 6) 5. request for member of non-aggregate type before '[' token (line 6) Basically, repeat that every time I have a for loop or an if statement. Can someone, please, tell my what I need to correct? Here is the class info for Item: :
|
Re: Overloading operator== for new class
this is a pointer, so line 4 of your first code snippet should look like this:
this->numbers.size() And you need to make all other references to this using pointer notation instead of dot notation too. |
Re: Overloading operator== for new class
The
this keyword has to be followed by -> and not ., as far as I know.edit: Ahh, dragon got it. |
Re: Overloading operator== for new class
Without addressing your question directly (a question that seems to have been addressed rather successfully), let me refer you to to a site, Mayuk's World.
Scorp hasn't visited us in a while, but contributions are often germane. |
Re: Overloading operator== for new class
Thank you all for the help on this. I've also found that removing this entirely has allowed it to compile as well (though I have yet to test it out; if it doesn't work, I'll try the "this->" thing. Again, thank you. :)
DaWei, I have the site bookmarked. This will make for some very good reading. Thank you. :) |
Re: Overloading operator== for new class
Hello again, I've got an odd problem. This too involves operator overloading, but this time, all the code for the overloading is syntactically correct.
Here's the thing: I have a Player class, which has a pointer to a Room class as a member variable. (This is so that I can assess what Room a Player is currently in, and it helps with command parsing.) In order to assign a Room to this pointer, however, I need to overload the operator=. Dev-C++ says that I should use: Room& Room::operator=(const Room&) I have written such code: :
The constructor for a Player looks like this: :
I'm not sure what my compiler wants from me. This is the only error, and it says I need "Room& Room::operator=(const Room&)", and I've written the code. So, what's wrong with this listing? Oh, and I'd like to point out that I have had no formal training in programming; I've been teaching myself through tutorials and books, and the part on Pointers has always been confusing to me. I know how they work, and why, it's just a little awkward getting the code to work. With no live people to ask, I'm kinda out of luck except the forums. So, I thank you greatly for your patience. :) EDIT: The exact error: Line 264 | rooms.hpp | no match for 'operator=' in '*((Player*)this)->Player::itsRoom = theRoom' note | rooms.hpp | candidates are: Room& Room::operator=(const Room&) |
Re: Overloading operator== for new class
theRoom is a pointer, so you need to dereference it to get at the value that it points to:
:
*itsRoom = *theRoom; |
Re: Overloading operator== for new class
Oh, I see. Thank you very much. :)
|
Re: Overloading operator== for new class
I don't believe that an array is an object and you can access members through it (function size()).
Here's my 'fixed code': :
BOOL Item::operator== (Item rhs) { |
Re: Overloading operator== for new class
@null_ptr0:
Quote:
Also note that the OP indicated that he fixed the problem from the answer provided in post #2 two days ago, so he probably doesn't need another answer. I know your keen to help, but you should at least read other people's responses - I noticed quite a few of your other posts are just rephrasing of answers already given. |
| All times are GMT -5. The time now is 3:25 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC