I have a static vector data member in my class and Im having trouble to initialize it. My vector's data type is another one of my classes which is called Leg. I've tried the ways I used with an int that worked. So not sure what is different about a vector.
#ifndef ShortestRoute_h
#define ShortestRoute_h
// ShortestRoute class definition
class ShortestRoute
{
public:
static vector<Leg> legVector;
ShortestRoute();
private:
};
// ShortestRoute constructor
ShortestRoute::ShortestRoute()
{
} // close constructor
const Leg Leg1("San Francisco", "Reno", 218.1);
vector<Leg> ShortestRoute::legVector.push_back( Leg1 );
#endif
also tried ShortestRoute::legVector.push_back( Leg1 );
here is this error:
1>ShortestRoute.cpp(156): error C2143: syntax error : missing ';' before '.'
1>ShortestRoute.cpp(156): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>ShortestRoute.cpp(156): error C2371: 'legVector' : redefinition; different basic types
1> ShortestRoute.cpp(143) : see declaration of 'legVector'
1>
You need to place the definition for legVector in the implementation (.cpp) file for your ShortestRoute class. You cannot push_back() elements outside of a function body.