Put object into array

I get an error when I try to put a class object into an array.

1
2
no operator "=" matches these operands 
operand types are: std::string = Foo 


My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
typedef unsigned short int USHORT;

class Foo
{
public:
	Foo();
	Foo(string itsName): Name(itsName) {}
	~Foo() {}

	void SetPos(string, Foo &);
	string GetPos(int x, int y) const { return Bar[x][y]; }

private:
	string Bar[8][8];
	string Name;
};

void Foo::SetPos(string Choice, Foo &Piece)
{
	if(Choice.find("Tess", 0) != string::npos)
	{
		for(USHORT i = 0; i < 8; i++)
			for(USHORT j = 0, a = 'A'; j < 8, a <= 'H'; j++, ++a)
			{
				if(Choice[5] == a && Choice[6] == (i+'1'))
				{
					Bar[i][j] = Piece; //Error on the equal sign here.         
					break;
				}
			}
	}
}


int main()
{	
	Foo myBoard;
	Foo Meep;

        myBoard.SetPos("Tess D3", Meep);

        return 0;
}


I don't know what the error means. Any help would be great!
You must create an overload of the operator = to suite your needs.

Aceix.
Bar is a string array and you try to put a foo inside.
I thought about overloading the operator. Problem is, I have no idea what I would put between the brackets.

@toum: So what should I make it? I've tried other types and still get errors.
Last edited on
"Piece" must be a string
If I make it a string, how would I send the object through?
What object ? What do you mean by "send through" ?
If I understood you correctly, you said to make Piece string here:

SetPos(string Choice, Foo &Piece)

to

SetPos(string Choice, string &Piece)

Then I pass Meep, the object, by reference in main():

1
2
3
Foo Meep;

        myBoard.SetPos("Tess D3", Meep);


This won't work if I put string &Piece.
It's obvious that Meep has to become a string.
But, I wanted to know how to store an object into the array. According to Aceix, I can do it if I overload the operator=, however; I don't know what I would put in the brackets for that.

Unless you're saying I can somehow do this: Foo string Meep, which doesn't make sense.
But, I wanted to know how to store an object into the array. According to Aceix, I can do it if I overload the operator=, however; I don't know what I would put in the brackets for that.

That's not what he meant. You can not store a Foo object in a string array. It's surprising you don't understand that.

If I try to understand your code, it seems like your Foo class is supposed to contain something like a chess board and the Bar array contains the name of the pieces for each position.
In that case the correct code is Bar[i][j] = Piece.Name;
But it's terrible design. There's absolutely no reason to use a Foo object - which contains a full board - to represent a piece when the only thing you want is the piece's name !


That's not the only thing I wanted, but I finally understand what you're saying. Thanks for the help!
Last edited on
Topic archived. No new replies allowed.