c++ - vector size reverts to 0 outside constructor -
i have class [network] during construction creates vector of objects [nodes]
inside constructor have declaration
vector<node> nodes (nodecount);
where nodecount int, lets 5
inside constructor can call
cout << nodes.size()
and output 5
here constructor
network::network() //establish initial node count network /* cout << "how many nodes: "; cin >> nodecount; */ nodecount = 5; vector<node> nodes (nodecount); cout << "nodes.size(): " << nodes.size() << endl; //initialize tables nodes for(int = 0; < nodes.size(); i++) { nodes[i].inittable(i, nodecount); //cout << "nodes[" << << "].table[0] - " << nodes[i].table[0] << endl; debug();
- pardon comments trying debug
but outside constructor if in function
void network::debug() cout << "nodecount: " << nodecount << endl; cout << "nodes.size(): " << getsize() << endl;
this output
nodecount: 5 nodes.size(): 0
i cannot figure out life of me why happening
it seems within constructor defined local variable
vector<node> nodes (nodecount);
that after exiting constructor destroyed.
you have use corresponding data member of class instead of local variable.