initializing static vector

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.

Thanks for any help in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <vector>
using std::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


#endif   



I've tried in main(), global & in constructor

1
2
3
4
5
ShortestRoute sR;
sR.legVector.push_back( Leg1 );
ShortestRoute::legVector.push_back( Leg1 );
vector<Leg> ShortestRoute::legVector.push_back( Leg1 );
ShortestRoute::legVector = aVector;


Don't know what's wrong or what im doing wrong.

Last edited on
u should initialise it immediately after the class definition
class name::ur vector initialisation
I tried this vector<Leg> ShortestRoute::legVector.push_back( Leg1 ); here is the code and the error:
1>ShortestRoute.cpp(156): error C2143: syntax error : missing ';' before '.'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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>


Thanks for the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<conio.h>
#include<vector>
using namespace std;


class abc
{
    public:
    int a;
    float b;
    abc()
    {
        a=0;
        b=1;
    }
};
class xyz
{
public:
 static vector<abc> myvector; 
   xyz();
};
std::vector<abc> xyz::myvector;
xyz::xyz()
{
}
void main()
{
    abc object;
    xyz::myvector.push_back(object);
}


Modify it as per ur program.If u have any doubts, pls feel free to ask.
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.
Last edited on
Thanks anirudh sn. That worked so I tried what you showed before except the
std::vector<Leg> ShortestRoute::legVector;. What does this do?

I'm working with one .cpp no .h's all classes are in the same .cpp as main() so no implementation file cire.

Anyway thanks for the help.
Last edited on

std::vector<Leg> ShortestRoute::legVector;

Since the vector is static it has to be initialised before use else it wont be assigned any memory.

std::vector<leg> informs the compiler about the type of static variable.
ShortestRoute::legVector vector initialisation with class name.
cool, Thanks for being helpfull.
Topic archived. No new replies allowed.