Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vector member of a class?

Status
Not open for further replies.

therick

Programmer
Mar 20, 2001
34
US
Having trouble using vector as a member of a class? Here's the code I'm using and it gives a "syntax error 'constant'" error on build. Possibly #include order problem or what i'm #including or definition of vector or that's it's a member of the class? I can't figure this out.

Here's the code:

//#pragma warning(disable:4786)

#include <iostream>
#include <vector>
#include <iterator>
#include <string>
#include <numeric>
#include <functional>

#if _MSC_VER > 1020
using namespace std;
#endif

class myclass {
public:
//int i;
vector <int> myvector(1);
};

int main (void) {
my class myobject;

return 0;
}






 
Your not allowed to use the vector constructor (besides the default) when defining a vector as a class memeber. The constant error is regarding the 1 you have passed to the vectors constructor. So take that out and work around it some other way.

bitwise
 
this is what you should do:

typedef vector<int> INTVECTOR;

class MyClass {

INTVECTOR yourVector;
...
}

You can use vectors with actually classes:

typedef vector<MyClass> MYCLASSVECTOR;

The only two things that you have to do are:
- operator=()
- copy constructor;


Good luck !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top