Jump to content
  • Advertisement
Sign in to follow this  
ontheheap

Accessing a vector of structs

This topic is 4972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Awesome! That did it [smile]. OK, I understand (better) how vectors work now. Whatever type of vector I declare, push_back requires an object of that type. Makes sense! Thanks agony.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!