1

In my header file, I have the following code

class ExEvent : public Event {
    public:
        ExEvent(
                Item* dst[SIZE],
                );
        ~ExEvent();
        Item* dst[SIZE];
};

In the cpp file, I have the following code

ExEvent::ExEvent(
        Item * dst[SIZE],
    ) : Event() {
    this->dst = &dst;
}

I get the following error:

error: array type 'Item *[15]' is not assignable
    this->dst = &dst;

Can someone explain why this error happens and why I cannot assign dst array pointer to this->dst.

3
  • You cannot directly assign arrays. Use std::copy instead, or even better use std::array in 1st place. Commented Nov 22, 2016 at 15:12
  • You have to copy the pointers to the array. You can't assign multiple pointers to a pointer array with '='. Use std::copy or memcpy. Commented Nov 22, 2016 at 15:19
  • You can eliminate the this-> syntax by giving your parameters different names than your member variables. Commented Nov 22, 2016 at 15:32

1 Answer 1

4

In function arguments type[any-size] is actually type*. I.e. ExEvent(Item*[SIZE]) is, in fact, ExEvent(Item**).

Hence, to fix the code:

ExEvent::ExEvent(Item* src[SIZE])
{
    std::copy_n(src, SIZE, this->dst);
}

Make sure that src has enough elements.

See declaring functions: parameter list for more details:

If the type is "array of T" or "array of unknown bound of T", it is replaced by the type "pointer to T"

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

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.