• Advertisement
Sign in to follow this  

Adding objects to vectors

This topic is 2593 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

Considering the following simple stat object:

struct Stat
{
unsigned short ID;
string Code;
string Name;
string Description;
}


How would I read values in from a text file and populate a vector of type Stat? I've tried variations of the following with no luck:


ifstream file;
string line;
size_t found;
int startPos = 1;
vector<Stat>::iterator i;

file.open(FILE_STATS); // defined constant for file name
if (file.is_open())
{
while (file >> line)
{
_stats.push_back(Stat());
i = _stats[_stats.size()];
found = line.find("|");
if (found != string::npos)
{
i.ID = (short int)line.substr(startPos, found - 1);
startPos = found + 1;
}
found = line.find("|", found);
if (found != string::npos)
{
i.Name = line.substr(startPos, found - startPos - 1);
startPos = found + 1;
}
i.Description = line.substr(startPos, line.length());
}



I'm sure part of the problem is my lack of knowledge in the use of vectors, so could someone help me out with their correct usage?

Share this post


Link to post
Share on other sites
Advertisement
So what's the problem? Compiler error? Runtime error? Little pink cats dancing on your monitor?

Share this post


Link to post
Share on other sites
Looks like a Lexing + parsing problem. Those topics are quite elaborate, or so I heard ;] Maybe you should have a look at them, but if you don't want to create your own, you can get a lexer and a parser and just learn how to use them.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
So what's the problem? Compiler error? Runtime error? Little pink cats dancing on your monitor?


Lol, I guess that might help. The error is as follows:

no match for ‘operator=’ in ‘i = ((GameData*)this)->GameData::_stats.std::vector<_Tp, _Alloc>::operator[] [with _Tp = Stat, _Alloc = std::allocator<Stat>](((GameData*)this)->GameData::_stats.std::vector<_Tp, _Alloc>::size [with _Tp = Stat, _Alloc = std::allocator<Stat>]())’

Honestly, I have little to no idea what this error means.

Share this post


Link to post
Share on other sites
i = _stats[_stats.size()];



this is wrong. First of all your indexing is out of bounds (+1). _stats[_stats.size()] will return a reference to an element in the vector and not an iterator. This is what gives you the compilation error.

You are also using dot to get the members of i. An iterator is similar to a pointer so you need to use an arrow, i->.

I don't think you need to use an iterator here at all. Use a reference instead.
Stat& i = _stats.back();


then you can still use dot to access the members.

Share this post


Link to post
Share on other sites
Thanks folks. I think I got it figured out. I was just making it more complicated than it needs to be. Now I'm simply creating a Stat object, modifying it's members and adding it to the vector with push_back(). Seems to work fine.

Share this post


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

  • Advertisement