nodes in vectors

This is a discussion on nodes in vectors within the C++ Programming forums, part of the General Programming Boards category; well seems like the number of problems i encounter never ends... I have a vector with nodes (each node contains ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    >> 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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    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!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21