• Advertisement
Sign in to follow this  

Weird error, store object into vector/list

This topic is 3561 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 got these weird errors when I store objects into a vector or list. I don't understand why because i've always done it in the past.
std::list<Player> players;

Player p(25,25); // create a player object
players.push_back(p); // store it into the list

// THIS doesn't work though and its annoying me
for (int i=0;i<players.size();i++)
    players.draw();



"error C2227: left of '.draw' must point to class/struct/union/generic type" That's probably the most relevant error-message I got from it. I don't get any compile-errors when I remove the for-loop and don't refer to the objects in the list. Thankful for help.

Share this post


Link to post
Share on other sites
Advertisement
std::list doesn't provide random access, I don't know what operator[] does (I didn't think it existed). You need to use iterators:

// Some typedefs to make things cleaner:
typedef std::list<Player> PlayerList;
typedef PlayerList::iterator PlayerListIt;


PlayerList players;

Player p(25,25); // create a player object
players.push_back(p); // store it into the list

for(PlayerListIt it=players.begin(); it!=players.end(); ++it)
it->draw();

Share this post


Link to post
Share on other sites
If I change the list to a vector I get the following errors:
main.cpp(92) : warning C4018: '<' : signed/unsigned mismatch
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: class Player & __thiscall std::vector<class Player,class std::allocator<class Player> >::operator[](unsigned int)" (??A?$vector@VPlayer@@V?$allocator@VPlayer@@@std@@@std@@QAEAAVPlayer@@I@Z)
fatal error LNK1120: 1 unresolved externals

That is why I tried a list instead. I changed the code to iterators in the list example and got the following similiar error:

1>main.obj : error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: class std::list<class Player,class std::allocator<class Player> >::_Const_iterator<1> & __thiscall std::list<class Player,class std::allocator<class Player> >::_Const_iterator<1>::operator++(void)" (??E?$_Const_iterator@$00@?$list@VPlayer@@V?$allocator@VPlayer@@@std@@@std@@QAEAAV012@XZ)
: fatal error LNK1120: 1 unresolved externals

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain_Thunder
This thread seems to have some more information on your error. It also might be helpful if you post the remainder of your source code.


Alright, I got it to work by changing back to Multi-threaded-debug-DLL. Still, there was a reason I removed that.

warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

At least it compiles now with only a warning.

Share this post


Link to post
Share on other sites
I use vectors in my code, so I can do players.at(i).draw(); It looks like "list" only allows the use of iterators. There are a selection of different STL containers, each is unique with its own pros and cons.

- Valles

Share this post


Link to post
Share on other sites
Quote:
Original post by password
Player p(25,25); // create a player object
players.push_back(p); // store it into the list

What actually happens is that a copy of p is stored into the list. So if you modifiy p later on, the Player object in the list won't be affected. I just thought I'd point that out.

Share this post


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

  • Advertisement