0

I want to allocate an array of C++ objects using the following code:

class myClass {
public:
    myClass(int userValue)
    : value(userValue)
    { }
}

private:
    int value;
};

int main(){
    myClass* objArray = new myClass(22)[5];

    return 0;
}

But it gives me the following error:

In constructor ‘myClass::myClass(int32)’:
error: expected ‘;’ before ‘[’ token
         objArray = new objArray(22)[5];

How should I create an array of objects then while passing parameters to them?

0

1 Answer 1

2

Use std::vector.

std::vector<myClass> objArray(5, 22);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But could you tell me the reason why it is not possible to allocate the objects array in regular way?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.