I'm trying to create a List for universal use within a class, but the compiler doesn't recognize the variables. Is there anything to be done here? Look at the last line of my class code:
It gets hung up on that last line. Here's the header:
Please help
For one, you don't need to store an iterator (except in cases where you do, but that's rarer).
The standard list usage is:
//we make it here for(std::list<Player>::iterator itr = mTiles.begin(); itr != mTiles.end(); itr++) { } //it goes away here
And what are you initializing iter to in the constructor here when you instantiate the class? (in Main, or whereever)
void Player::Initialize(int x, int y, ALLEGRO_BITMAP* image, std::list<Tile*> Tiles, std::list<Tile*>::iterator iter)
This is initialized to a list of Tile objects (used for the background grid). What I'm trying to do here is create a list inside the class that can be used by the entire class but that can be used to accept a list that is passed in via Initialize, which is where you see the following:
mTiles = Tiles;
mIter = iter;
Maybe I'm wrong and just misunderstanding what you're doing. I just want to make sure you know you can do this:
my_function(std::list<int> *temp) //or pointer and change the dots to -> { for(std::list<int>::iterator itr = temp.begin() ; itr != temp.end() itr++) { //no need to explicitly pass an iterator, you can generate one. //blah } } int main() { std::list<int> my_list; my_function(my_list); }
OH, but I think I know what's blowing up in your code:
int collide(int footX, int footY) { for (Player::mIter = mTiles.begin(); mIter != mTiles.end(); ++iter)
Collide is NOT a member function, which you may have intended that way, but it would probably also work inside your object as a member function.
Player::mIter is asking for an iterator to a CLASS TYPE, not an INSTANCE of that class. You CAN have static methods associated with a class type, but it's most likely not what you want to do here. Static methods are shared by all instantiated objects of that class type, but they can be dangerous, confusing, or useless depending on what you're doing.
You want something more akin to:
Player player1;//somewhere void collide() { for (player1.mIter = mTiles.begin(); mIter != mTiles.end(); ++iter) //... }
However, it would still be better to have collide() as a member function.
Player player1;//somewhere void Player::collide() { //NO need for Player::mIter or player1.mIter it's already in our object. for (mIter = mTiles.begin(); mIter != mTiles.end(); ++iter) //... }
Look carefully at the names of the iterators in each section of the for statement.
You're incrementing iter instead of mIter, and testing against mIter, which never changes, hence the infinite loop.
DUH!!! I didn't make it a class member function. I'm good now, thanks.