The following code generates an error when the

//vector<int> myVofInt2(n);

line is uncommented. I get: error C2059: syntax error :'constant'.

Does anyone know why it treats the declaration/initializations differently when the vector is inside a class?

The vector<int> in the main function creates a vector<int> with ten elements, which is what I want to do, but I need the object initialized inside a class. I've tried putting the initialization in the class constructor, but that has not worked either.

class myplayclass
{
public:
	int x;
	int y;
	vector<int> myVofInt2(10);
	myplayclass(){}
	~myplayclass(){}
	myplayclass(int intX):x(intX){y=0;}
};

int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myVofInt(10);

	return 0;
}

I would use the class constructor to initialize your vector to the appropriate number of elements.

commented: thank you +1

Well I can't declare and initialize in the class constructor or else the vector will only exist for the scope of the constructor(instead of the class). I would still have to declare the object in the class and then either push_back objects after calling reserve (or something similar) in the class constructor.

////////////////////////////////////////////////
This is actually part of a larger problem. I was trying to boil it down to its essence.

I am trying to create a vector of objects. Speed is the issue. It is very fast when I can simply do:

vector[myclass] v1(10);

This creates and initializes a vector of ten myclass objects. The first myclass object is created using the myclass constructor and then the myclass copy constructor is used to fill the remaining 9 elements. It is lightning fast, but it doesn't work when I encapsulate the line within another class.

This does not work either.

vector<int> myVofInt;
myVofInt(10);

Poster above you was correct, please look at how this is different from what you did.

class myplayclass
    {
    public:
    int x;
    int y;
    vector<int> myVofInt2;
    myplayclass() : myVofInt2(10) {} // you can call its constructor like this
    ~myplayclass(){}
    myplayclass(int intX):x(intX), myVofInt2(10) {y=0;}
    };
     
    int _tmain(int argc, _TCHAR* argv[])
    {
    vector<int> myVofInt(10);
     
    return 0;
    }
commented: Thank you very much, I didn't realize there was a difference +1

You can't initialize values inside a class' definition. You have to do it within the class' constructor(s). For a vector, you would simply declare the vector, then in the constructor you would call either vector::resize or vector::reserve. Which you use depends on how you plan to use the vector later. You might be able to do it in your initializer list too, but I'm not sure...
Header:

class Example {
 private:
   vector<int> myIntVec;
 public:
   Example();
};

*.cpp file:

Example::Example() {
  myIntVec.resize(10);
}

/* !!!OR!!! */

Example::Example() {
  myIntVec.reserve(10);
}

Which version of the constructor you use depends on if you are planning on using the subscript operator (operator[]) or if you are planning on using vector::push_back() to populate the vector. You would use the first version (w/ vector::resize) for subscripts and the second version (w/ vector::reserve) for using vector::push_back.

EDIT:
Hmmm.... I'm not sure if this is relevant any more. I overlapped a couple posts.

Well I can't declare and initialize in the class constructor or else the vector will only exist for the scope of the constructor(instead of the class). I would still have to declare the object in the class and then either push_back objects after calling reserve (or something similar) in the class constructor.

////////////////////////////////////////////////
This is actually part of a larger problem. I was trying to boil it down to its essence.

I am trying to create a vector of objects. Speed is the issue. It is very fast when I can simply do:

vector[myclass] v1(10);

This creates and initializes a vector of ten myclass objects. The first myclass object is created using the myclass constructor and then the myclass copy constructor is used to fill the remaining 9 elements. It is lightning fast, but it doesn't work when I encapsulate the line within another class.

This does not work either.

vector<int> myVofInt;
myVofInt(10);

Are you referring to a static class member?

thank you guys for getting me straightened out. This all arose because I didn't understand constructor semantics. I thought

class mytestclass{
	public:
		int x;
		mytestclass(){x=0;}//default constructor
	};

// Was the same as:

	class mytestclass{
	public:
		int x; int y; 
		mytestclass():x(0){} //default constructor
	};

But now I see the difference. In the constructor that which follows the colon is what initializes the members, and that which is contained in the curly brackets is similar to that which is inside the curly brackets of a regular function. Well, at least that's my understanding of it all now. Solving this small problem made coding for my larger problem a piece of cake. Thank you again.

@gerard, no I was not referring to a static class member. I do not reckon they can be instantiated in a constructor, same for const? Oh well, probably topic for another thread. I marked this one as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.