How to declare a vector

Hi im trying to declare a vector, i.e. in the .h file...

vector<TaxPayer> taxpayers;

however i want to declare the vetor to b of size 20 and thought you could just write

vector<TaxPayer> taxpayers(20);

however this causes error, should i instantiate in the .cpp instead and if so how do i do what i want above

thanks
Vector's are dynamically resized when required. But to accomplish what you want to do in your constructor put

taxpayers.resize(20);
You can specify a size at declaration, but your TaxPayer must have a default (no arguments) constructor.

(I think)
One thing, though.
It is possible to declare the vector in the header so that it's available to everyone that includes it and still give it size 20 without calling resize(). Just put
extern vector<TaxPayer> taxpayers;
in the header, and
vector<TaxPayer> taxpayers(20);
in some .cpp. Preferably one that is related to the header, but where it is doesn't matter as long as it appears only once.
Last edited on
@helios: No, plus that's really a bad practice to declare it as extern (global). You should encapsulate it within an object. And if need be, make that object a singleton.
Well, OP said he wanted to declare the vector in the header, and the only reason one would want to do that is to make the vector global. Unless of course he meant he wanted it as a class member.
Considering the vector is to hold a user-type of "TaxPayer". It'd be a fair assumption that he wanted it as a class member.

I'd be willing to bet that there is no need to declare its size at all. Just let it do its own thing...
@seymore15074: If you need a high level of optimization, and you know the maximum it's going to hold. Then declaring the size helps with speed, so it's not continually re-allocating.

The other reason is if you want to treat it as an array without adding elements to it. Resize it so you can directly access the elements without having to use push_back etc.
hi and thanks Zaita at least you make a bit of sense...
the other lad helios tends to confuse more then help everytime...

yeah its meant to b a member variable and not a class member, im only new to c++ and our lecture told us to declare everything in (inside) the header ie
vector<TaxPayer>_ taxpayers;
and then instantiate it in the constructor in the .cpp file
but I have to instantiate it so that its created with size 20 but dont know how to do this as
vector<TaxPayer>_ taxpayers(20);
causes error's, so hopefully how could tell me how and were to declare and initialize my vector _taxpayers to be of size 20...

many thanks
Considering the vector is to hold a user-type of "TaxPayer". It'd be a fair assumption that he wanted it as a class member.

How come? Having a global vector of objects or a vector of objects class member both make as much sense to me.

Pat: You know, I'm only trying to help. If my suggestion was irrelevant you don't need to say so. It's your own fault for not giving such important details as the scope of the vector or what the error was.
<rant>Now that I think about it, a lot of people these days like to say "an error". Are we supposed to deduce what the error is, or use some kind of unholy divination technique, or something? Seriously, is it so hard to write down the specific error before posting? Jesus Christ.</rant>

Leaving that aside, is that misplaced underscore a typo when you posted, or is it in the source, too? My guess is that this is want you want to do:
1
2
3
4
A::A():
_taxpayers(20){
	//...
}

Other than, your only other choice is this:
1
2
3
4
A::A(){
	_taxpayers.resize(20)
	//...
}

Note that if TaxPayer doesn't have a constructor that takes no parameters, any of these will fail.
Last edited on
This usage is kind of strange in my opinion, but I think this is what you need:

Test.H:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TEST_H
#define TEST_H

#include <vector>

#include "TaxPayer.H"

using std::vector;

struct Test
{
    vector<TaxPayer> _taxpayers;
    Test();
};

#endif /* TEST_H */ 


Test.C:
1
2
3
4
5
6
#include "Test.H"

Test::Test()
  : _taxpayers( 20 )  // constructs a vector of 20 empty TaxPayers
{
};


Once you instantiate an object of this class you can assign values to the members of _taxpayers[0-19]. Note that accessing _taxpayers[20+] will cause a segmentation fault.

I got beat to it.
Last edited on
Helios, please, that is the name of my God. Can we please avoid abusing him in a programming forum?

And Pat, helios posts here because he likes to help. Please don't abuse him for trying. If he confuses you, ask for clarification or just plain ignore him. Snide, third-person comments are unnecessary. He, at least, is smart enough to have native proficiency in C++ and in English.

Far more people read these threads than post. Let's be civil and prove that we have bigger brains than normal --rather than dispel the myth.
Your god's name is Helios? Geez. Talk about forgetting to update.
Note: on the slim chance that you actually do believe in Greek mythology, the above comment is intended to be purely humorous.

Well, I only respond to offenses. (Normally.)


Never mind.
Last edited on
I think he was referring to:
Jesus Christ
Ah, crap.
How come? Having a global vector of objects or a vector of objects class member both make as much sense to me.


Global objects break the rules of encapsulation.
I wrote a test program with a few classes to try the vector's constructor in this case. What is unusual is that the constructor of the TaxPayer class is not being called, but the vector allocates the right amount of space for them.

Doesn't that sound a bit abnormal?


Nevermind. A clean build solves everything...
Last edited on
Yeah. Because global objects are never appropriate.
*cough* std *cough* cout *cough*
Topic archived. No new replies allowed.