0

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

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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();
};

4 Comments

Yes, but that opens up the possibility of memory leaks... I'd recommend avoiding pointers unless necessary.
@Roddy If I could -1 your comment I would. Just because pointers carry a small 'risk' if you don't know what you're doing doesn't mean you shouldn't use them. There are many many advantages to using pointers.
@Konrad - I'm not saying 'never use pointers' - I agree 100% pointers are essential in many cases. But, you shouldn't use them "unless necessary" as I wrote. Otherwise, you'd always use for (int * foo=new(0); (*foo) < 10; (*foo)++)... :-) The OP seems confused about objects vs. pointers, so it's better to keep it simple to begin. And just making spielfeld an array of pointers DOES give a memory leak!
@Roddy. Point taken. Consider my criticism cheerfully withdrawn :-)
0

The = operator defined in feld wants a (const) reference to feld and you are passing it a pointer to feld

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.