Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2003
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    (C++) vector of struct objects

    I have defined this in my program:

    class myClass{

    string name;

    struct theStruct{
    int data;
    myClass *link;
    };

    vector<theStruct> info;

    };

    Suppose I want to insert the number '5' into the data variable of the struct, how would I refer to it inside a member function? I've tried "info->data", "info.data". It feels obvious but I don't know it. Thanks.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    info is an instance of vector (I'm guessing STL?). So to access it's members, you need to use one of vector's methods. I'm not very familiar with STL, but it should be something along the lines of:
    Code:
    info.getElement(something).data;
    or:
    Code:
    info.getElement(something)->data;
    getElement() is just a guess, look at the vector template for the real method you'll need there.
    But something looks off to me in your implementation. Shouldn't the link member of the struct point to another struct and not the class?

    shmoove

  • #3
    New to the CF scene
    Join Date
    May 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the syntax to access an element in a vector is vectorname.at(index number).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •