Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with classes.

This thread is locked; no one can reply to it. rss feed Print
Problem with classes.
Gnamra
Member #12,503
January 2011
avatar

I've got this Entity class
edit:entire entity.h class

#SelectExpand
1#ifndef ENTITY_H 2#define ENTITY_H 3 4#include <allegro5\allegro.h> 5#include <allegro5\allegro_primitives.h> 6#include <allegro5\allegro_image.h> 7 8#include "Tower.h" 9#include "Level.h" 10#include "Tile.h" 11 12class Entity 13{ 14public: 15 Entity(); 16 Entity(int h, int a) : 17 health(h), 18 attack(a){}; 19 20 int getHealth(){ 21 return health; 22 }; 23 void setHealth(int h){ 24 health = h; 25 }; 26 27 int getAttack(){ 28 return attack; 29 }; 30 void setAttack(int a){ 31 attack = a; 32 }; 33 34 ALLEGRO_BITMAP* getSkin(){ 35 return skin; 36 }; 37 void setSkin(ALLEGRO_BITMAP *bitmap){ 38 skin = bitmap; 39 }; 40 41private: 42 float posX, posY; 43 int health, speed, attack; 44 ALLEGRO_BITMAP *skin; 45}; 46 47#endif

and this GunTower class

edit: entire Tower.h file

#SelectExpand
1#ifndef TOWER_H 2#define TOWER_H 3 4#include "Entity.h" 5#include "Level.h" 6 7#include "allegro5\allegro.h" 8 9class noTower : public Entity 10{ 11public: 12 noTower(); 13}; 14 15class GunTower : public Entity 16{ 17public: 18 GunTower(int Nbr, int h, int a, float fireRate) 19 { 20 setUpgradeNbr(Nbr); 21 setHealth(h); 22 setAttack(a); 23 setFireRate(fireRate); 24 setSkin(Level::gunTower); 25 }; 26 27 int getUpgradeNbr(){ 28 return upgradeNbr; 29 }; 30 int setUpgradeNbr(int nbr){ 31 upgradeNbr = nbr; 32 }; 33 34 void setFireRate(float rate){ 35 fireRate = rate; 36 }; 37 float getFireRate(){ 38 return fireRate; 39 }; 40 41private: 42 43 int upgradeNbr; 44 float fireRate; 45}; 46 47class CannonTower : public Entity 48{ 49public: 50 CannonTower(int Nbr, int h, int a, float fireRate) 51 { 52 setUpgradeNbr(Nbr); 53 setHealth(h); 54 setAttack(a); 55 setFireRate(fireRate); 56 //setSkin(Level::cannonTower); 57 }; 58 59 int getUpgradeNbr(){ 60 return upgradeNbr; 61 }; 62 int setUpgradeNbr(int nbr){ 63 upgradeNbr = nbr; 64 }; 65 66 void setFireRate(float rate){ 67 fireRate = rate; 68 }; 69 float getFireRate(){ 70 return fireRate; 71 }; 72 73private: 74 int upgradeNbr; 75 float fireRate; 76}; 77 78class LaserTower : public Entity 79{ 80public: 81 LaserTower(int Nbr, int h, int a, float fireRate) 82 { 83 setUpgradeNbr(Nbr); 84 setHealth(h); 85 setAttack(a); 86 setFireRate(fireRate); 87// setSkin(Level::laserTower); 88 }; 89 90 int getUpgradeNbr(){ 91 return upgradeNbr; 92 }; 93 int setUpgradeNbr(int nbr){ 94 upgradeNbr = nbr; 95 }; 96 97 void setFireRate(float rate){ 98 fireRate = rate; 99 }; 100 float getFireRate(){ 101 return fireRate; 102 }; 103 104private: 105 int upgradeNbr; 106 float fireRate; 107}; 108 109#endif

Level.h

#SelectExpand
1#ifndef LEVEL_H 2#define LEVEL_H 3 4#include "Tile.h" 5#include "allegro5\allegro.h" 6#include "allegro5\allegro_primitives.h" 7#include "allegro5\allegro_image.h" 8 9class Level 10{ 11public: 12 Level(){}; 13 static ALLEGRO_BITMAP *gunTower; 14 static ALLEGRO_BITMAP *cannonTower; 15 static ALLEGRO_BITMAP *laserTower; 16 static void loadBitmaps(); 17 static void fillLevel(); 18 static void changeTile(int index_y, int index_x, int towerId) 19 { 20 map[index_y][index_x].setTowerId(towerId); 21 }; 22 static Tile map[10][16]; 23}; 24 25#endif

level.cpp

#SelectExpand
1#include "Level.h" 2 3ALLEGRO_BITMAP* Level::gunTower; 4ALLEGRO_BITMAP* Level::cannonTower; 5ALLEGRO_BITMAP* Level::laserTower; 6Tile Level::map[10][16]; 7 8void Level::loadBitmaps() 9{ 10 Level::gunTower = al_load_bitmap("1.png"); 11 Level::cannonTower = al_load_bitmap("2.png"); 12 Level::laserTower = al_load_bitmap("3.png"); 13 14}; 15 16void Level::fillLevel() 17{ 18 for(int y = 0; y < 10; y++){ 19 for(int x = 0; x < 16; x++){ 20 Level::map[y][x] = Tile(x * 40, y * 40, 0, true); 21 }; 22 }; 23};

main.cpp

#SelectExpand
1#include <iostream> 2#include <vector> 3#include "allegro5\allegro.h" 4#include "allegro5\allegro_primitives.h" 5#include "allegro5\allegro_image.h" 6#include "Level.h" 7#include "Tower.h" 8#include "Entity.h" 9#include "Tile.h" 10#include "Enemies.h" 11 12using namespace std; 13 14 15int main() 16{ 17 al_init(); 18 al_init_primitives_addon(); 19 al_init_image_addon(); 20 21 bool running = true; 22 bool draw = false; 23 int mousex = 0; 24 int mousey = 0; 25 int selectedTower = 0; 26 27 ALLEGRO_DISPLAY *display = NULL; 28 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); 29 ALLEGRO_TIMER *drawTimer; 30 31 display = al_create_display(640, 440); 32 drawTimer = al_create_timer(1.0/60.0); 33 34 al_install_keyboard(); 35 al_install_mouse(); 36 al_register_event_source(event_queue, al_get_keyboard_event_source()); 37 al_register_event_source(event_queue, al_get_mouse_event_source()); 38 al_register_event_source(event_queue, al_get_timer_event_source(drawTimer)); 39 40 Level::loadBitmaps(); 41 Level::fillLevel(); 42 43 al_start_timer(drawTimer); 44 while(running) 45 { 46 ALLEGRO_EVENT ev; 47 al_wait_for_event(event_queue, &ev); 48 if(ev.type == ALLEGRO_EVENT_TIMER && al_is_event_queue_empty(event_queue)) draw = true; 49 if(ev.type == ALLEGRO_EVENT_KEY_DOWN && al_is_event_queue_empty(event_queue)) 50 if(ev.keyboard.keycode == ALLEGRO_KEY_Q) running = false; 51 52 if(ev.type == ALLEGRO_EVENT_MOUSE_AXES){ 53 mousey = ev.mouse.y; 54 mousex = ev.mouse.x; 55 }else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){ 56 mousey = ev.mouse.y; 57 mousex = ev.mouse.x; 58 59 if(mousex >= 280 && mousex <= 400 && mousey >= 400 && mousey <= 440) 60 selectedTower = (mousex / 40) - 6; 61 //else if(mousex >= 0 && mousex <= 640 && mousey >= 0 && mousey < 400) 62 // if(selectedTower > 0 && selectedTower < 4); 63 64 }; 65 66 if(draw) 67 { 68 draw = false; 69 al_clear_to_color(al_map_rgb(0,0,0)); 70 71 for(int y = 40; y <= 400; y += 40){ 72 for(int x = 40; x <= 640; x += 40){ 73 al_draw_line(x,y-40,x,y,al_map_rgb(255,0,0), 2); 74 al_draw_line(x-40,y,x,y,al_map_rgb(255,0,0), 2); 75 } 76 } 77 78 al_draw_filled_rectangle((mousex/40)*40, (mousey/40)*40, (mousex/40)*40+40, (mousey/40)*40+40, al_map_rgb(0,0,255)); 79 al_draw_bitmap(Level::gunTower, 640/2-40, 400,0); 80 al_draw_bitmap(Level::cannonTower, 640/2, 400,0); 81 al_draw_bitmap(Level::laserTower, 640/2+40, 400,0); 82 al_draw_rectangle(40*(6+selectedTower), 400, 40*(6+selectedTower)+40, 440, al_map_rgb(255,0,0), 2); 83 84 al_flip_display(); 85 } 86 }; 87 88 89 return 0; 90;}

Tile.h

#SelectExpand
1#ifndef TILE_H 2#define TILE_H 3 4#include "TowerManger.h" 5 6class Tile 7{ 8public: 9 Tile(){}; 10 Tile(int x, int y, int tId, bool empty) : 11 posX(x), 12 posY(y), 13 towerId(tId), 14 isEmpty(empty){}; 15 16 void setTowerId(int id){ 17 towerId = id; 18 19 }; 20 int getTowerId(){ 21 return towerId; 22 }; 23 24 void setIsEmpty(bool b){ 25 isEmpty = b; 26 }; 27 bool getIsEmpty(){ 28 return isEmpty; 29 }; 30 31private: 32 int towerId; 33 bool isEmpty; 34 int posX, posY; 35}; 36 37 38#endif

Towermanager.h

#SelectExpand
1#ifndef TOWERMANAGER_H 2#define TOWERMANAGER_H 3 4#include "Tower.h" 5#include "Level.h" 6#include "Entity.h" 7#include "Tile.h" 8using namespace std; 9 10class TowerManager 11{ 12public: 13 static Entity towerMngr[10][16]; 14 static void addTower(int towerID, int posX, int posY){ 15 switch(towerID){ 16 case 1: 17 towerMngr[posY/40][posX/40] = GunTower(1, 100, 50, 1); 18 break; 19 case 2: 20 towerMngr[posY/40][posX/40] = CannonTower(1, 100, 100, 0.5); 21 break; 22 case 3: 23 towerMngr[posY/40][posX/40] = LaserTower(1, 100, 1, 100); 24 break; 25 }; 26 }; 27 static void removeTower(int posX, int posY){ 28 towerMngr[posY/40][posX/40] = noTower(); 29 }; 30}; 31 32#endif

In the constructor, it fails to compile at the setSkin function/method.

The errors are:
Error 1 error C2653: 'Level' : is not a class or namespace name
Error 2 error C2065: 'gunTower' : undeclared identifier
Error 3 error C2653: 'Level' : is not a class or namespace name
Error 4 error C2065: 'gunTower' : undeclared identifier

What am I missing? I see no reason for it to not find it, I've included the necessary header files.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Gnamra
Member #12,503
January 2011
avatar

I updated the first post, copy pasted the header files and cpp files.

Edit: I found the problem, it was circular includes.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Yeah, there it is. tower.h includes entity.h which includes tower.h, so on.... entity.h doesnb't need the include to tower.h there. A base class rarely needs to know about derived classes, if it does, it uses pointers to itself usually... anyway, done rambling now....

tile.h said:

#include "TowerManger.h"

Misspelled Manager there...

Otherwise, it looks okay.

jmasterx
Member #11,410
October 2009

You know it's Christmas when you misspell Manager for Manger ;)

Arthur Kalliokoski
Second in Command
February 2005
avatar

jmasterx said:

You know it's Christmas

Better hurry! Only 363 shopping days left!

They all watch too much MSNBC... they get ideas.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

jmasterx
Member #11,410
October 2009

Miss Pell, the hottest 3rd and 141516ths grade teacher that ever lived [in our minds] 8-)

AWishart_Miss_Pell.jpg

video

Go to: