Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » What am I doing wrong here?

This thread is locked; no one can reply to it. rss feed Print
What am I doing wrong here?
agonvs
Member #15,917
March 2015

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:

#SelectExpand
1#include<iostream> 2#include<list> 3#include"player.h" 4#include"globals.h" 5#include"tile.h" 6using namespace std; 7 8void Player::Initialize(int x, int y, ALLEGRO_BITMAP* image, std::list<Tile*> Tiles, std::list<Tile*>::iterator iter) 9{ 10 mX = x; 11 mY = y; 12 mVelX = 0; 13 mVelY = 0; 14 bottomFrame = 1; 15 topFrame = 0; 16 tint = false; 17 flip = false; 18 mFacing = RIGHT; 19 mDir = EAST; 20 mImage = image; 21 mTiles = Tiles; 22 mIter = iter; 23 idleCount = 0; 24 idleIndex = 1; 25 mJumpInts[0] = -16; 26 mJumpInts[1] = -16; 27 mJumpInts[2] = -16; 28 mJumpInts[3] = -16; 29 mJumpInts[4] = -8; 30 mJumpInts[5] = -8; 31 mJumpInts[6] = -8; 32 mJumpInts[7] = -8; 33 mJumpInts[8] = -4; 34 mJumpInts[9] = -4; 35 mJumpInts[10] = -4; 36 mJumpInts[11] = -4; 37 mJumpInts[12] = -2; 38 mJumpInts[13] = -2; 39 mJumpInts[14] = -2; 40 mJumpInts[15] = -2; 41 mJumpInts[16] = -1; 42 mJumpInts[17] = -1; 43 mJumpInts[18] = -1; 44 mJumpInts[19] = -1; 45 mJumpInts[20] = -1; 46 mJumpInts[21] = -1; 47 mJumpInts[22] = -1; 48 mJumpInts[23] = -1; 49 mJumpInts[24] = 0; 50 mJumpInts[25] = 0; 51 mJumpInts[26] = 0; 52 mJumpInts[27] = 0; 53 mJumpInts[28] = 0; 54 mJumpInts[29] = 0; 55 mFallInts[0] = 0; 56 mFallInts[1] = 0; 57 mFallInts[2] = 0; 58 mFallInts[3] = 0; 59 mFallInts[4] = 0; 60 mFallInts[5] = 0; 61 mFallInts[6] = 1; 62 mFallInts[7] = 1; 63 mFallInts[8] = 1; 64 mFallInts[9] = 1; 65 mFallInts[10] = 1; 66 mFallInts[11] = 1; 67 mFallInts[12] = 1; 68 mFallInts[13] = 1; 69 mFallInts[14] = 2; 70 mFallInts[15] = 2; 71 mFallInts[16] = 2; 72 mFallInts[17] = 2; 73 mFallInts[18] = 4; 74 mFallInts[19] = 4; 75 mFallInts[20] = 4; 76 mFallInts[21] = 4; 77 mFallInts[22] = 8; 78 mFallInts[23] = 8; 79 mFallInts[24] = 8; 80 mFallInts[25] = 8; 81 mFallInts[26] = 16; 82 mFallInts[27] = 16; 83 mFallInts[28] = 16; 84 mFallInts[29] = 16; 85} 86 87void Player::Update(direction dir, ALLEGRO_JOYSTICK_STATE jst) 88{ 89 if (dir == EAST) 90 east(); 91 if (dir == NONE) 92 none(); 93 if (dir == WEST) 94 west(); 95 if (justFell && !(jst.button[0])) 96 justFell = false; 97 if (!(justFell) && jst.button[0]) 98 jump(); 99} 100 101void Player::Render() 102{ 103 if (mFacing == RIGHT) 104 { 105 al_draw_bitmap_region(mImage, (bottomFrame*128), 0, 128, 128, mX, mY, 0); 106 al_draw_bitmap_region(mImage, (topFrame*128), 0, 128, 128, mX, mY, 0); 107 } 108 else 109 { 110 al_draw_bitmap_region(mImage, (bottomFrame*128), 0, 128, 128, mX, mY, ALLEGRO_FLIP_HORIZONTAL); 111 al_draw_bitmap_region(mImage, (topFrame*128), 0, 128, 128, mX, mY, ALLEGRO_FLIP_HORIZONTAL); 112 } 113} 114 115void Player::east() 116{ 117 mFacing = RIGHT; 118 int tile = collide(102, 122); 119 if (tile == 1) 120 { 121 walk(); 122 fallIndex = 0; 123 } 124 else if (tile == 2) 125 mVelX = 0; 126 else 127 fall(); 128} 129 130void Player::west() 131{ 132 mFacing = LEFT; 133 int tile = collide(32, 122); 134 if (tile == 1) 135 { 136 walk(); 137 fallIndex = 0; 138 } 139 else if (tile == 2) 140 mVelX = 0; 141 else 142 fall(); 143} 144 145void Player::none() 146{ 147 if (mFacing == RIGHT) 148 { 149 int tile = collide(102,122); 150 if (tile == 1) 151 idle(); 152 else 153 fall(); 154 } 155 else 156 { 157 int tile = collide(32,122); 158 if (tile == 1) 159 idle(); 160 else 161 fall; 162 } 163} 164 165void Player::idle() 166{ 167 if (idleCount == 5) 168 { 169 idleCount = 0; 170 if (idleIndex == 4) 171 idleIndex = 1; 172 else 173 idleIndex++; 174 } 175 idleCount++; 176} 177 178int collide(int footX, int footY) 179{ 180 for (Player::mIter = mTiles.begin(); mIter != mTiles.end(); ++iter)

It gets hung up on that last line. Here's the header:

#SelectExpand
1//#include "entity.h" 2#include <allegro5\allegro.h> 3#include <list> 4#include "globals.h" 5#include "tile.h" 6 7enum facing {RIGHT, LEFT}; 8 9class Player 10{ 11private: 12 std::list<Tile*> mTiles; 13 std::list<Tile*>::iterator mIter; 14 int mX, mY, mVelX, mVelY, bottomFrame, topFrame, fallIndex, idleCount, idleIndex; 15 bool tint, flip, justFell; 16 int mJumpInts[30]; 17 int mFallInts[30]; 18 facing mFacing; 19 direction mDir; 20 ALLEGRO_BITMAP* mImage; 21 ALLEGRO_COLOR color; 22public: 23 int getVelX(){return mVelX;} 24 int getVelY(){return mVelY;} 25 int collide(int footX, int footY); 26 void west(); 27 void none(); 28 void east(); 29 void idle(); 30 void walk(); 31 void jump(); 32 void fall(); 33 void shoot(); 34 void Initialize(int x, int y, ALLEGRO_BITMAP* image, std::list<Tile*> Tiles, std::list<Tile*>::iterator iter); 35 void Update(direction dir, ALLEGRO_JOYSTICK_STATE jst); 36 void Render(); 37 bool collide(int x, int y, int width, int height);

Please help ;D

Chris Katko
Member #1,881
January 2002
avatar

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)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

agonvs
Member #15,917
March 2015

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;

Chris Katko
Member #1,881
January 2002
avatar

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)
//...
}

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

agonvs said:

#SelectExpand
180 for (Player::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.

agonvs
Member #15,917
March 2015

DUH!!! I didn't make it a class member function. I'm good now, thanks. ::)

Go to: