1

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
Columbo
  • 2,896
  • 7
  • 44
  • 54
  • You dont need to reserve space in a vector as it will resize when you do the push_back – Lodle Jun 23 '09 at 12:06
  • Not necessary, but a good practice if you know - at least approximately - the number of elements in advance because it reduces the number of buffer reallocations and associated copying. – sharptooth Jun 23 '09 at 12:08
  • If you're worried about good practice, then don't allocate vectors with new anyway... – Steve Jessop Jun 23 '09 at 12:11
  • Can I just ask why it is bad to allocate vectors with NEW? – Columbo Jun 23 '09 at 12:50
  • It's not disastrous, but two issues. First it ties you to vector, when your caller might prefer deque or list, or to process the results in streaming mode. To avoid this, you can provide some dependency injection by writing template code that takes an output iterator, and lets your caller manage the scope of the underlying collection (if any). Second, C++ gives you mechanisms (via RAII) to make resource management more robust: exception-safe, less prone to programmer error, etc. Allocating anything (not just vector) with new and returning the pointer ignores all this. – Steve Jessop Jun 23 '09 at 13:29
  • Oh yes, and for vector in particular, if you're storing the pointer to vector in an object, then usually you might as well just have a vector in the object directly. An empty vector isn't much bigger than a pointer, and embedding means less cleanup code. Likewise if your caller can put the vector on its stack and pass it to you by reference (or a back_inserter, as I said already). – Steve Jessop Jun 23 '09 at 13:34

4 Answers4

9

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]);
}
heavyd
  • 17,303
  • 5
  • 56
  • 74
1

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

sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

Insterad of

vVec.reserve(frankReservedSpace);

you want:

vVec->reserve(frankReservedSpace);
0

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561