I'm designing a class that has a std::vector<int>
as an instance variable. I'm using a std::vector
because I need to set its size at runtime. Here are the relevant portions of my code:
my_class.h: #include <vector> using std::vector; class MyClass { int size; vector<int> vec; } my_class.cc: #include "my_class.h" using std::vector MyClass::MyClass(int m_size) : size(m_size) { vec = new vector<int>(size,0); }
When I attempt to compile I get these error messages:
g++ -c -Wall my_class.cc -o my_class.o my_class.cc: In constructor ‘MyClass::MyClass(int): my_class.cc:4 error: no match for ‘operator=’ in ‘((MyClass*)this)->My_Class::vec = ((*(const allocator_type*)(& std::allocator<int>())), (operator new(24u), (<statement>, ((std::vector<int>*)<anonymous>))))’ make: *** [my_class.o] Error 1
However, when I change the offending line to:
vector<int> temp(size,0); vec = temp;
It now compiles without a hitch and I get the desired behavior and can access my vector as
vec[i] // i having been defined as an int yada yada yada
This workaround is okay, but I would like to understand why it works and the first method fails. Thanks in advance.
You can initialize a vector by using an array that has been already defined. You need to pass the elements of the array to the iterator constructor of the vector class. The array of size n is passed to the iterator constructor of the vector class.
Begin Declare a class named as vector. Declare vec of vector type. Declare a constructor of vector class. Pass a vector object v as a parameter to the constructor.
Syntax for Vectors in C++Every new vector must be declared starting with the vector keyword. This is followed by angle brackets which contain the the type of data the vector can accept like strings, integers, and so on.
Just do:
MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0)
You already seem to know about initializer lists, why not initialize vector there directly?
vec = new vector<int>(size,0);
is illegal because new
returns a pointer and in your case vec
is an object.
Your second option:
vector<int> temp(size,0); vec = temp;
although it compiles, does extra work for no gain. By the time you reach the assignment, two vectors would already have been constructed and discarded afterwards.
The use of vector is legal in your class, the problem is how you initialize it:
#include <vector> class MyClass { public: MyClass(int m_size); // ... more things... private: int size; vector<int> vec; }
You are assigning a pointer to a new vector object, as if this vector object was not initialized.
vec = new vector<int>(size,0);
If you really want this to work, then you should declare your vec
object as:
vector<int> * vec;
And don't forget to add a destructor:
MyClass::~MyClass { delete vec; }
Why did it work when you dropped the new
particle? Because you are creating a new object vector
, and overwriting the one in your class (this does not guarantee the original one to be correctly eliminated, however).
You actually don't need to do that. Your vector
object is already initialized (its default constructor called) when you've reached the constructor of MyClass. If you just want to be sure that memory is reserved for size
items:
MyClass::MyClass(int m_size): size(m_size) { vec.reserve( size ); }
If you want your vector to have size
elements, then:
MyClass::MyClass(int m_size): size(m_size), vec(m_size, 0) {}
Finally, as one of the commenters points out, size is not actually needed once the vector has been constructed. So you can get rid of the size
member:
class MyClass { public: MyClass(int m_size): vec(m_size, 0) {} unsigned int getSize() const { return vec.size(); } // ... more things... private: vector<int> vec; }
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With