...

View Full Version : (C++) vector of struct objects



loonatik
04-18-2004, 12:28 AM
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.

shmoove
04-18-2004, 09:22 AM
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:


info.getElement(something).data;

or:


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

MikeBennett
05-25-2004, 10:08 PM
the syntax to access an element in a vector is vectorname.at(index number).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum