Columbo
Columbo

Reputation: 2936

manipulation of Vectors created with new

Can anyone help with this...

vector<unsigned int> *vVec = new vector<unsigned int>;
        vVec .reserve(frankReservedSpace);

        start = std::clock();
        for(int f=0; f<sizeOfvec; f++)
        {   //Populate the newly created vector on the heap
            vVec .push_back(pArray[f]);
        }

I'm getting: error C2228: left of '.reserve' must have class/struct/union

I'm creating a vector using the new operator so that it outlives the function where it is created. This therefore gives me back a pointer to that vector on the heap rather than an actual vector object itself. therefore it won't let me carry out any .reserve() of push_backs. I can't see a way around it, can anyone help?

Upvotes: 1

Views: 239

Answers (4)

heavyd
heavyd

Reputation: 17731

vVec is a pointer to a vector. Therefore you should be using the indirection (->) operator rather than the dot (.)

vector<unsigned int> *vVec = new vector<unsigned int>;
vVec->reserve(frankReservedSpace);

start = std::clock();
for(int f=0; f<sizeOfvec; f++)
{  //Populate the newly created vector on the heap
   vVec->push_back(pArray[f]);
}

Upvotes: 9

duffymo
duffymo

Reputation: 308898

Personally, I don't care for the Hungarian notation you've build into your variable name. I would rather see something more domain specific and self-documenting than 'vVec'. If you decided to change to a linked list, would the variable name have to change to reflect that? Obviously the answer is no.

Upvotes: 0

anon
anon

Reputation:

Insterad of

vVec.reserve(frankReservedSpace);

you want:

vVec->reserve(frankReservedSpace);

Upvotes: 0

sharptooth
sharptooth

Reputation: 170509

Use "->" instead of "." vVec is of pointer type, so you need to use operator -> to access members of the object it points to.

Upvotes: 1

Related Questions