Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Overloading operator== for new class (http://www.programmingforums.org/showthread.php?t=14518)

TimothyBennett Nov 19th, 2007 2:11 PM

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:

:

  1. bool Item::operator==(Item rhs)
  2. //Check all member variables for equality, then return the result.
  3. {
  4.       for(int i = 0; i < this.numbers.size(); i++)
  5.       {
  6.             if(this.numbers[i] != rhs.numbers[i])
  7.                   return false;
  8.       }
  9.       for(int i = 0; i < this.settings.size(); i++)
  10.       {
  11.             if(this.settings[i] != rhs.settings[i])
  12.                   return false;
  13.       }
  14.       if(this.getUsable() != rhs.getUsable())
  15.             return false;
  16.       return true; //All checks are made, we're clear.
  17. }




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:
:

  1. class Item
  2. {
  3.       public:
  4.             Item(string name, string desc, int num, int batt, int code);
  5.  
  6.             vector<string> settings;
  7.  
  8.             vector<int> numbers;
  9.  
  10.             //Accessor
  11.             flag getUsable() { return usable; }
  12.  
  13.             //For Comparison purposes
  14.             bool operator==(Item rhs);
  15.       private:               
  16.             flag usable;
  17. };


Ancient Dragon Nov 19th, 2007 3:18 PM

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.

peaceofpi Nov 19th, 2007 3:20 PM

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.

DaWei Nov 19th, 2007 3:44 PM

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.

TimothyBennett Nov 19th, 2007 5:48 PM

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. :)

TimothyBennett Nov 21st, 2007 3:12 PM

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:

:

  1. Room & Room::operator=(const Room & rhs)
  2. {
  3.       if(this == &rhs)
  4.       {
  5.             return *this;
  6.       }
  7.       else
  8.       {
  9.             //Get all the member variables to equal rhs
  10.             for(int i = 0; i < itsItems.size(); i++)
  11.             {
  12.                   itsItems[i] = rhs.getItems()[i];
  13.             }
  14.             for(int i = 0; i < itsInteracts.size(); i++)
  15.             {
  16.                   itsInteracts[i] = rhs.getInteracts()[i];
  17.             }
  18.             location = rhs.getLocation();
  19.             return *this;
  20.       }
  21. }


The constructor for a Player looks like this:

:

  1. Player::Player(Room *theRoom)
  2. {
  3.       *itsRoom = theRoom;
  4. }


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&)

The Dark Nov 21st, 2007 4:32 PM

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;

TimothyBennett Nov 21st, 2007 5:19 PM

Re: Overloading operator== for new class
 
Oh, I see. Thank you very much. :)

null_ptr0 Nov 21st, 2007 9:26 PM

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) {
        for(int i = 0; i < (sizeof(this->numbers) / sizeof(int)) + (sizeof(this->settings) / sizeof(int)); i++)
                if(i < sizeof(numbers) / sizeof(int))
                        if(this->numbers[i] != rhs.numbers[i])
                                return 0;
                else
                        if(this->settings[i] != rhs.settings[i])
                                return 0;
        return this->getUsable() == rhs.getUsable();
}


The Dark Nov 21st, 2007 10:27 PM

Re: Overloading operator== for new class
 
@null_ptr0:
Quote:

I don't believe that an array is an object and you can access members through it (function size()).
You are right in that you can't access members (such as size()) in an array. However, you wrong in thinking that numbers is an array. It is a vector, which is an entirely different animal.
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