-
nodes in vectors
well seems like the number of problems i encounter never ends...
I have a vector with nodes (each node contains several items, say x,y,z). I am trying to read the values in each node, and I can't succeed (it compiles and links, but the output is 1 all the time). Here's what I did:
Code:
class block_class
{
public:
block_class();
block_class( etc...);
int get_x() { return x;};
int get_y() { return y;};
int get_z() { return z;};
etc...
private:
int x,y,z;
}
Code:
// the driver function
vector<block_class> list;
block_class A;
...fill in block A (assign values to x,y,z) and do list.push_back(A)...
...fill in block A again with different values and do list.push_back(A)...
...do the above for a few times until the list vector is populated with block_classes...
and now I want to do something like list[0].get_x or list.at[0].get_x and I can't. So what I did was:
Code:
block_class X = list[0];
cout<<X.get_x;
which keeps returning "1"
Please advise.
-
>> cout<<X.get_x;
I assume you mean cout<<X.get_x();.
>> I want to do something like list[0].get_x or list.at[0].get_x and I can't.
Why can't you? It should work (with the parentheses of course) to do list[0].get_x() or list.at(0).get_x().
With such a simple issue, you should be able to make a complete program with that class and a main that has your vector, some population code, and the incorrect output. Try making such a program and if you don't find the problem while doing that, then you can post it so we can see and/or run it ourselves.
-
sir, i really appreciate your help. my friend just realized that the prob was caused by shallow copying in another func. thank you for your lightning responses tho!!