0

I am a beginner in C++ that confused over dynamic array. I found a way to do it in this site, but I can't seem to make it work for "array of object" in an object. It always break when I read the Rec variable.

This is the class definition:

class AlcXIO {
private:
    AlcX_IO_Record* Rec[1];
    int _Count;
public: 
    int count();
    void Init(CL_DisplayWindow window);
    void AddInput(int IO_ID);
    AlcX_IO_Record* GetRec(int RecID);
    void on_input_down(const CL_InputEvent &key, const CL_InputState &state);
    void on_input_up(const CL_InputEvent &key, const CL_InputState &state);
};

The AddInput() function:

void AlcXIO::AddInput(int IO_ID) {
    size_t newSize = this->_Count +1;
    AlcX_IO_Record* newArr = new AlcX_IO_Record[newSize];

    memcpy( newArr, Rec, _Count * sizeof(AlcX_IO_Record) );

    _Count = newSize;
    delete [] Rec;
    Rec[0] = newArr;
}

I aware that I'm probably wrong on: Rec[0] = newArr

But changing it to Rec = newArr gives me an error: "expression must be a modifiable lvalue"

Any solution is welcome, thank you.

1 Answer 1

1

Remove the [1] from the declaration, you're using it as a pointer, not an array of pointers as it is declared now. The error stems from trying to overwrite the fact that you declared it as an array - the type cannot be changed programmatically at runtime, even though it's technically compatible.

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

3 Comments

it give me "error C2229: class 'AlcXIO' has an illegal zero-sized array" :( EDIT: changing it to AlcX_IO_Record* Rec; gives me another error "error C2100: illegal indirection"
You removed only the 1, remove the square brackets as well.
changing it to AlcX_IO_Record* Rec; gives me another error "error C2100: illegal indirection" on Rec = newArr;

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.