Sign in to follow this  
ontheheap

Accessing a vector of structs

Recommended Posts

I have a class named CFont; CFont contains a public struct named _Rect. I have a private vector of type _Rect (vector<_Rect> Rect). _Rect consists of four ints (x, y, w, h). CFont also has two other private vectors (vector<int> key and vector<char> data). I'm filling the vectors from data inside a file, using the push_back() function. I'm able to fill the key and data vectors with no problem, but I can't figure out how to access the members of Rect! Basically, I need to do something like:

fin >> number;
Rect.x.push_back(number);

// etc
Perhaps this isn't even possible with vectors, I'm not really sure. The error I'm getting is: font.cpp:36: error: 'class std::vector<CFont::_Rect, std::allocator<CFont::_Rect> >' has no member named 'x' I'm still learning all this C++ OO goodness, and C++'s other neato things like std::vector, so there's a good chance I'm trying to do something that isn't supposed to be done [grin]. Any help would be appreciated.

Share this post


Link to post
Share on other sites
i read through your post a few times, but its kind of hard to follow.. could you write out exactly what your CFont class looks like?

from your example, it appears you're trying to call the 'push_back' method, however, 'x' is not a std::vector, but an (i assume) integer.

like i said, not sure what your trying to do but, why not

fin >> Rect.x;

?

Share this post


Link to post
Share on other sites
Sure.

font.h


class CFont : public CSDLImage
{

public:

CFont( std::string bitmap, std::string description );
CFont( std::string bitmap, std::string description, int r, int g, int b );

char lookUpKey( int k );
void printString( const char* s, int x, int y );

struct _Rect {

int x;
int y;
int w;
int h;
};


private:

std::vector<int> key;
std::vector<char> data;
std::vector<_Rect> Rect;
};




font.cpp (only the constructor)

CFont::CFont(std::string bitmap, std::string description, int r, int g, int b)
: CSDLImage( bitmap, r, g, b )
{
int numberOfChars;
std::ifstream fin( description.c_str() );
fin >> numberOfChars;

for(int i = 0; i < numberOfChars; i++)
{
int number;
char letter;
fin >> number; key.push_back(number);
fin >> letter; data.push_back(letter);
fin >> number; Rect.x.push_back(number);
fin >> number;
fin >> number;
fin >> number;
}

fin.close();
}




Basically, my font class holds all information and functions needed to make SDL font printing a hell of a lot easier (without needing to use any extra libs).

Share this post


Link to post
Share on other sites
push_back() excepts an object of type _Rect. It won't accept anything else. So read all your values into a temporary _Rect structure, and then push the structure onto the vector.

Also, push_back() is a member function of vector, so it has to be called from a vector object. Rect is a vector object. It has no members called x, y, w, or h. The elements contained within it do, since they are of type _Rect. But Rect itself is a vector.


for(int i = 0; i < numberOfChars; i++)
{
char letter;
_Rect TempRect;
fin >> number; key.push_back(number);
fin >> letter; data.push_back(letter);
fin >> TempRect.x;
fin >> TempRect.y;
fin >> TempRect.w;
fin >> TempRect.h;
Rect.push_back(TempRect);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this