Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this allocation of a vector not accepted?

So I have declared a vector in my class header like this:

 ...
 private:
    vector<Instruction> instructions;
 ...

Then in the .cpp implementation in the constructor, I try to initialize it like this:

 instructions = new vector<Instruction>();

Xcode tells me: No viable overloaded '='

I am basically trying to get this class to behave like I would expect in java, where instances of the class retain this vector. Thats why I wanted to dynamically allocate it using new, so as to make sure that it doesn't get lost on the stack or something. Any help would be appreciated with this, thanks so much.

like image 729
wfbarksdale Avatar asked Dec 03 '25 19:12

wfbarksdale


1 Answers

In order to do what you're trying to do the instructions = new vector<Instruction>() line is entirely unnecessary. Simply remove it. The vector will automatically get default-constructed when an instance of your class gets constructed.

An alternative is to make instructions into a pointer, but there doesn't appear to be any reason to do this here.

like image 113
NPE Avatar answered Dec 06 '25 08:12

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!