2 Replies - 651 Views - Last Post: 13 November 2011 - 03:53 AM Rate Topic: -----

#1 CrazyKatWilliams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-November 11

Overloading 'operator=' doesn't seem to work; help please?

Posted 13 November 2011 - 12:25 AM

Hello; I'm trying to create a program that would display a 2D map of characters. However, some of the characters are 'animated', such that every 3 seconds their character representation changes.
It's probably because it's early morning, but I just can't seem to get my operator= correct.
Here's the applicable code:
//For loop that displays the current map
      for(k = 0; k<5; k++)
      {
          cout << testTile[k].id << "  ";
          if(testTile[k].nextAnim!= NULL) testTile[k] = testTile[k].nextAnim;   //if it is an animated tile, change it to the tile it points to
      }


//////////////////////////////
tile.h
//The tiles header file

struct tile
{
  char id;
  tile *nextAnim;     
  
  tile tile::operator=(const tile& a) 
  {
   id = a.id;
   return *this;
    }
  
};







Any ideas? Here is my error(s);
41 C:\Dev-Cpp\main2.cpp no match for 'operator=' in 'testTile[k] = testTile[k].tile::nextAnim'
error C:\Dev-Cpp\structs.h:9 candidates are: tile tile::operator=(const tile&)

Is This A Good Question/Topic? 0
  • +

Replies To: Overloading 'operator=' doesn't seem to work; help please?

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Overloading 'operator=' doesn't seem to work; help please?

Posted 13 November 2011 - 02:12 AM

tile tile::operator=(const tile& a) - this is how the function header should look when you implement the function outside the struct. Otherwise, it the scope resolution operator mustn't be here ( :: ). So: tile operator=(const tile &a) should do the trick. But there's still one problem, as your operator= should return a reference, to allow operator chaining:
tile a, b, c, d;
// can't do this if operator= doesn't return a reference
a = b = c = d;


so, use: tile &operator=(const tile &a);
More info on operator overloading.

This post has been edited by sarmanu: 13 November 2011 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5820
  • View blog
  • Posts: 12,670
  • Joined: 16-October 07

Re: Overloading 'operator=' doesn't seem to work; help please?

Posted 13 November 2011 - 03:53 AM

Interesting. First, you don't really need to override the operator= is you're just looking to assign.

for(k = 0; k<5; k++) {
	cout << testTile[k].id << "  ";
	tile *next = testTile[k].nextAnim;
	if(next!=NULL) {
		testTile[k] = *next;
	}
}



However, it's unclear what you're after. You have a tile and it has a linked list of possible values it can be? Both my code and yours essentially loose that.

Give your tile more brains! Let it state it's job clearly and handle it for you.

e.g.
class tile {
public:
	tile();
	tile(char);
	~tile();
	char getId() const;
	void moveNext();
	void add(char);
private:
	char id;
	tile *next;     
};


for(k = 0; k<5; k++) {
	cout << testTile[k].getId() << "  ";
	testTile[k].moveNext();
}



Actually, that's a poor road map. The public stuff is fine, but your implementation will be messy. I'd go with:
class tile {
public:
	tile();
	tile(char);
	char getId() const;
	void moveNext();
	void add(char);
private:
	std::vector<char> ids;
	int currentIndex;
};



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1