Reputation: 5881
I working on a game but I have a problem with the initialization of the level. (feld is just field in german)
class level{
private:
feld spielfeld[10][10];
public:
/*
other stuff
*/
void init_feld();
};
void level::init_feld()
{
for(int i=0;i!=10;i++){
for(int n=0;n!=10;n++){
spielfeld[i][n] = new feld(land, i, n);
}
}
}
The Error:
Error: no match for »operator=« in »((level*)this)->level::spielfeld[i][n] = (operator new(24u), (, ((feld*))))« /home/nick/stratego/feld.h:18:11:
Remark: candidate is: feld& feld::operator=(const feld&) Process terminated with status 1 (0 minutes, 0 seconds) 2 errors, 0 warnings
Upvotes: 0
Views: 292
Reputation: 4484
The other answers are correct in that you have a type mismatch in your use of the assignment operator. The error message can be explained as saying that the feld class has no assignment operator (operator=) that takes a pointer to a feld (feld *). The candidate function it is suggesting is likely the default assignment operator (unless you have implemented your own operator=).
So as the other answers suggest, you could change the type of the spielfeld to take pointers to feld objects or you could remove the use of operator new when you assign the felds to the array (of course this would then allocate them on the stack, which you may not want if they are large in size).
Upvotes: 0
Reputation: 68023
You're using "new", but feld
isn't an array of pointers, it's an array of objects.
Two choices -
Make it an array of pointers. feld * spielfeld[10][10]
Beware that if you do this, you've now got to worry about deleting the objects which are created by 'new'.
Or, leave it as an array of objects (no wories about 'delete#) and initialize them differently.
spielfeld[i][n] = feld(land, i, n); // uses assignment operator
This isn't the most efficient or elegant way as intermediate feld
items get constructed and deleted, but it should work. Depending on what values feld contains you might need to write an assignment operator....
Upvotes: 0
Reputation: 2352
The = operator defined in feld wants a (const) reference to feld and you are passing it a pointer to feld
Upvotes: 0
Reputation: 791361
spielfeld[i][n]
is a feld
object, new feld(land, i, n)
dynamically allocates a new feld
object and returns a pointer to that object. If you want to assign to a feld
value to spielfeld[i][n]
you could use:
spielfeld[i][n] = feld(land, i, n);
Alternatively you may be able to set the appropriate members of spielfeld[i][n]
directly or using other member functions.
Upvotes: 6
Reputation: 37427
You need to declare an array of array of pointers to feld.
class level{
private:
feld * spielfeld[10][10]; // <-- Added pointer to here
public:
/*
other staff
*/
void init_feld();
};
Upvotes: 1