How to intialize vector using constructor

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using namespace std;
static int index = 0;
class neighbor
{
    public:
            int dest ;
            int weight;
            neighbor( int d= 0, int w= 0);
};
template<typename T>
class vertexInfo
{
    public:
            enum vertexColor { white , grey , black } ;
            typename  map< T, int>:: iterator vtxMapLoc;
            set<neighbor> edges;
             vertexInfo()
             {
                //Default constructor
             }
             vertexInfo( const map<T, int> :: iterator& iter)
             {
                // constructor with iterator pointing to a vertex in map   
             }
             int intDegree;
             int outDegree;
};
template <typename T>
class graph
{
    private:

            typename  map< T, int > :: iterator iter;
            map <T,int> vtxMap;
            vector<vertexInfo<T> > vInfo;
            int numVertices;
            int numedges;
            stack<int> availStack;
            int getvInfoIndex(graph<T>& , const T& v);
    public:
            void addEdge( graph<T>& , const T&  , const T&  , int );
            set<T> get_Neighbor( graph<T>& , const T& v) ;
            void show(graph<T>& );

};

template <typename T>
void graph<T> :: addEdge ( graph<T> & g, const T& v1 , const T& v2, int w)
{
    pair <map <char, int> :: iterator, bool> ret ;

     ret = g.vtxMap.insert(pair <char, int >( v1, index));

     if( ret.second)
     {
        index++;
        g.vInfo.push_back(index));--> Error ( so added constructor)

      }

     ret = g.vtxMap.insert(pair <char, int >(v2 , index));

     if( ret.second)
     {
        index++;
        g.vInfo.push_back(index);--> Error ( so added constructor) 
      }
   }
Last edited on
You do not have a constructor that accepts index. Use ret.first instead.

If you had an explicit constructor you can use emplace() or emplace_back(). See:

http://www.cplusplus.com/reference/vector/vector/emplace_back/
I think that graph<T>::addEdge and other graph member functions should not be trying to access a second graph. It's either g.show() or show(g), not g1.show(g2).
Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/


The iterators that you do want to store ... can they become invalid in some operation? If yes, will you take appropriate measures to avoid dereferencing invalid iterators?
@ coder777
here I want to create constructor.
In the book they have mentioned like this
VertexInfo()
// default constructor

vertecInfo( const map<T,int> :: iterator iter)
// constrctor with iterator pointing to a vertex in map

But not given body of constructor.
How to write that ??
@ cubbi
the error is because constructor has not written. So I can not push "index" in vector vInfo
in book only this much written

VertexInfo()
// default constructor

vertecInfo( const map<T,int> :: iterator iter)
// constrctor with iterator pointing to a vertex in map

But not given body of constructor.
Can you help me ??
I would guess that the vtxMapLoc should be initialized:
1
2
3
4
             vertexInfo( const map<T, int> :: iterator& iter) : vtxMapLoc{iter}
             {
                // constructor with iterator pointing to a vertex in map   
             }
Topic archived. No new replies allowed.