Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[C++] Problem when including a header

Started by RazZziel Oct 31, 2006 at 6:13 AM 2 replies 700+ views
Original Post
RazZziel
RazZziel
Hi, I've got a weird error when including a header file. In my game, I have theese files: CBall.h:
#ifndef __CBALL_H__
#define __CBALL_H__


#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"

#include "CGame.h"
#include "CVector.h"
#include "CAnimation.h"


enum {
        POS_CENTER = -1
};

/* Files */

// Pixmaps

#define P_BALL_ZERG                             "data/pixmap/ball/zerg_ball_spin.png"
#define P_BALL_BIRTH_ZERG               "data/pixmap/ball/zerg_ball_birth.png"
#define P_BALL_HIDE_ZERG                "data/pixmap/ball/zerg_ball_hide.png"
#define P_BALL_UNHIDE_ZERG              "data/pixmap/ball/zerg_ball_unhide.png"

#define P_BALL_TERRAN                   "data/pixmap/ball/terran_ball_spin.png"
#define P_BALL_BIRTH_TERRAN             "data/pixmap/ball/terran_ball_birth.png"

#define P_BALL_PROTOSS                  "data/pixmap/ball/protoss_ball_spin.png"
#define P_BALL_BIRTH_PROTOSS    "data/pixmap/ball/protoss_ball_birth.png"


// Sounds

#define S_ZERG_START                    "data/sound/ball/zerg_ball_start.wav"
#define S_ZERG_READY                    "data/sound/ball/zerg_ball_ready.wav"
#define S_ZERG_BIRTH                    "data/sound/ball/zerg_ball_birth.wav"
#define S_ZERG_BOUNCE                   "data/sound/ball/zerg_ball_bounce.wav"

#define S_TERRAN_START                  "data/sound/ball/terran_ball_start.wav"
#define S_TERRAN_READY                  "data/sound/ball/terran_ball_ready.wav"
#define S_TERRAN_BIRTH                  "data/sound/ball/terran_ball_birth.wav"
#define S_TERRAN_BOUNCE                 "data/sound/ball/terran_ball_bounce.wav"

#define S_PROTOSS_START                 "data/sound/ball/protoss_ball_start.wav"
#define S_PROTOSS_READY                 "data/sound/ball/protoss_ball_ready.wav"
#define S_PROTOSS_BIRTH                 "data/sound/ball/protoss_ball_birth.wav"
#define S_PROTOSS_BOUNCE                "data/sound/ball/protoss_ball_bounce.wav"


typedef enum {
        NORMAL,
        EGG,

        HIDDING,
        HIDDEN,
        UNHIDDING
} BallStatus;


typedef enum {
        CMB_NULL,

        CMB_NEUT_1,
        CMB_NEUT_2,
        CMB_NEUT_3,
        CMB_NEUT_4,

        CMB_ZERG_1,
        CMB_ZERG_2,
        CMB_ZERG_3,


        CMB_TERR_1,
        CMB_TERR_2,
        CMB_TERR_3,
        CMB_TERR_4,

        CMB_TOSS_1,
        CMB_TOSS_2,
        CMB_TOSS_3
} Combo;


class Ball {

        protected:
                //Game *game;

                Animation *ani_spin, *ani_birth, *ani_hide, *ani_unhide;
                Mix_Chunk *s_birth, *s_bounce;

                int race;
                bool birth_sound;
                BallStatus status;

                // Combo stuff
                Combo effect;
                int effect_way;
                float steps;
                float y0;
                Vector or_p;
                float def_vx;

        public:
                Vector p;
                Vector v;
                dot way;
                int r;
                int m;
                Mix_Chunk *s_start, *s_ready;

                Ball(int x, int y, int vx, int vy/*, Game *game*/);
                ~Ball();

                void show();
                void sayStart();
                void sayReady();

                void setEffect(Combo effect, int effect_way);

                inline void birth()
{ birth_sound = true; effect = CMB_NULL; status = EGG; }
                inline void resetPosition()    { p.x = or_p.x, p.y = or_p.y; }
                inline void setPosition(int x, int y)   { p = Vector(x, y); }
                inline void setVelocity(int vx, int vy) { v = Vector(vx, vy); }
                inline void accelerate(int ax, int ay)  { v += Vector(ax, ay); }

};

class BallZerg : public Ball {
        public:
                BallZerg(int x, int y, int vx, int vy/*, Game *game*/);
};



class BallTerran : public Ball {
        public:
                BallTerran(int x, int y, int vx, int vy/*, Game *game*/);
};
class BallProtoss : public Ball {
        public:
                BallProtoss(int x, int y, int vx, int vy/*, Game *game*/);
};


#endif
CGame.h:
#ifndef __CGAME_H__
#define __CGAME_H__


#include <vector>

#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"

#include "CFramerate.h"
#include "CBall.h"
#include "CPlayer.h"



#define P_BACKGROUND    "data/pixmap/game/bg_zerg.png"

#define P_LIFE_EMPTY    "data/pixmap/game/life_empt.png"
#define P_LIFE_FULL             "data/pixmap/game/life_full.png"

enum {
        LIFE_OFFSET     = 5,
        LIFE_STEP       = 6,
        LIFE_MAX        = 34,
        LIFE_Y          = SCREEN_HEIGHT/15,
        LIFE_X0         = SCREEN_WIDTH*11/40,
        LIFE_X1         = SCREEN_WIDTH*29/40
};

#define P_COVERCAT              "data/pixmap/game/covercat.png"
#define P_COVERSNAKE    "data/pixmap/game/coversnake.png"

/* Sounds */

#define S_FLASH1                "data/sound/game/intro_flash1.wav"
#define S_FLASH2                "data/sound/game/intro_flash2.wav"

#define M_RAZIOZERG             "data/sound/music/radiofreezerg.ogg"

enum {
        GAME_FPS = 60
};

class Game {

        private:
                Player *players[2], *winner, *loser;
                SDL_Surface *background, *life_empty, *life_full;
                SDL_Surface *covercat, *coversnake;
                Mix_Music *s_music;
                std::vector<Ball*> balls;
                bool multiplayer;

                int life_x0, life_x1;
                int round, rounds;

                SDL_Rect life[2];

                void draw_player_stats();
                void setup_player_stats();
                void intro();
                void round_title();
                bool game_loop();
                void end();

        public:
                Game(Player *player1, Player *player2, int rounds);
                ~Game();

                void configure_multiplayer();
                void start();

};


#endif
So, if in CBall.h, line 8, I don't #include "CGame.h", the game compiles and runs perfectly. But now I need to have a refference to the game object inside each ball (in order to change the state of the game when certain events happen to the ball), so I included "CGame.h", without using any Game object inside CBall.h or CBall.cpp (all refferences are commented out). Then, when I try to make my source, I get this:

cd '/home/raziel/Programacion/Projects/sc_pong' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" gmake -k
gmake all-recursive
Making all in src
if g++ -DHAVE_CONFIG_H -I. -I/home/raziel/Programacion/Projects/sc_pong/src
-I.. -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -D__LINUX__ -D__PC__ -O0 
-pedantic -Wall -g3 -MT CBall.o -MD -MP -MF ".deps/CBall.Tpo" -c -o CBall.o CBall.cpp;
then mv -f ".deps/CBall.Tpo" ".deps/CBall.Po"; else rm -f ".deps/CBall.Tpo"; exit 1; fi
if g++ -DHAVE_CONFIG_H -I. -I/home/raziel/Programacion/Projects/sc_pong/src
-I.. -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -D__LINUX__ -D__PC__ -O0 
-pedantic -Wall -g3 -MT CPlayer.o -MD -MP -MF ".deps/CPlayer.Tpo" -c -o CPlayer.o CPlayer.cpp;
then mv -f ".deps/CPlayer.Tpo" ".deps/CPlayer.Po"; else rm -f ".deps/CPlayer.Tpo"; exit 1; fi
if g++ -DHAVE_CONFIG_H -I. -I/home/raziel/Programacion/Projects/sc_pong/src
-I.. -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -D__LINUX__ -D__PC__ -O0 
-pedantic -Wall -g3 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.cpp;
then mv -f ".deps/main.Tpo" ".deps/main.Po"; else rm -f ".deps/main.Tpo"; exit 1; fi
CGame.h:69: error: ISO C++ forbids declaration of 'Player' with no type
CGame.h:69: error: expected ';' before '*' token
CGame.h:73: error: 'Ball' was not declared in this scope
CGame.h:73: error: template argument 1 is invalid
CGame.h:73: error: template argument 2 is invalid
CGame.h:89: error: expected `)' before '*' token
gmake[2]: *** [main.o] Error 1
if g++ -DHAVE_CONFIG_H -I. -I/home/raziel/Programacion/Projects/sc_pong/src
-I.. -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -D__LINUX__ -D__PC__ -O0 
-pedantic -Wall -g3 -MT CGame.o -MD -MP -MF ".deps/CGame.Tpo" -c -o CGame.o CGame.cpp;
then mv -f ".deps/CGame.Tpo" ".deps/CGame.Po"; else rm -f ".deps/CGame.Tpo"; exit 1; fi
CGame.h:69: error: ISO C++ forbids declaration of 'Player' with no type
CGame.h:69: error: expected ';' before '*' token
CGame.h:73: error: 'Ball' was not declared in this scope
CGame.h:73: error: template argument 1 is invalid
CGame.h:73: error: template argument 2 is invalid
CGame.h:89: error: expected `)' before '*' token
CGame.cpp:52: error: prototype for 'Game::Game(Player*, Player*, int)'
does not match any in class 'Game'
CGame.h:66: error: candidates are: Game::Game(const Game&)
CGame.h:66: error: Game::Game()
CGame.cpp: In constructor 'Game::Game(Player*, Player*, int)':
CGame.cpp:59: error: 'players' was not declared in this scope
CGame.cpp: In member function 'void Game::setup_player_stats()':
CGame.cpp:101: error: 'players' was not declared in this scope
CGame.cpp: In member function 'void Game::draw_player_stats()':
CGame.cpp:132: error: 'players' was not declared in this scope
CGame.cpp:136: error: 'players' was not declared in this scope
CGame.cpp: In member function 'void Game::start()':
CGame.cpp:167: error: request for member 'push_back' in '((Game*)this)->Game::balls', which is of non-class type 'int'
CGame.cpp:183: error: 'winner' was not declared in this scope
CGame.cpp:185: error: 'loser' was not declared in this scope
CGame.cpp:188: error: 'players' was not declared in this scope
CGame.cpp:202: error: 'players' was not declared in this scope
CGame.cpp: In member function 'void Game::intro()':
CGame.cpp:232: error: invalid types 'int[int]' for array subscript
CGame.cpp:241: error: 'players' was not declared in this scope
CGame.cpp: In member function 'void Game::round_title()':
CGame.cpp:306: error: invalid types 'int[int]' for array subscript
CGame.cpp:324: error: 'players' was not declared in this scope
CGame.cpp: In member function 'void Game::end()':
CGame.cpp:367: error: 'winner' was not declared in this scope
CGame.cpp:367: error: 'loser' was not declared in this scope
CGame.cpp: In member function 'bool Game::game_loop()':
CGame.cpp:402: error: 'winner' was not declared in this scope
CGame.cpp:403: error: 'loser' was not declared in this scope
CGame.cpp:418: error: 'players' was not declared in this scope
CGame.cpp:421: error: 'players' was not declared in this scope
CGame.cpp:428: error: 'players' was not declared in this scope
CGame.cpp:429: error: no matching function for call to 'Physics::checkBallCollisions(int*)'
CPhysics.h:50: note: candidates are: void Physics::checkBallCollisions(std::vector<Ball*, std::allocator<Ball*> >*)
CGame.cpp:451: error: request for member 'size' in
'((Game*)this)->Game::balls', which is of non-class type 'int'
CGame.cpp:453: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:453: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:464: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:465: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:466: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:468: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:468: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:479: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:480: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:481: error: invalid types 'int[Uint8]' for array subscript
CGame.cpp:484: error: invalid types 'int[Uint8]' for array subscript
gmake[2]: *** [CGame.o] Error 1
if g++ -DHAVE_CONFIG_H -I. -I/home/raziel/Programacion/Projects/sc_pong/src
-I.. -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -D__LINUX__ -D__PC__ -O0 
-pedantic -Wall -g3 -MT menus.o -MD -MP -MF ".deps/menus.Tpo" -c -o menus.o menus.cpp;
then mv -f ".deps/menus.Tpo" ".deps/menus.Po"; else rm -f ".deps/menus.Tpo"; exit 1; fi
if g++ -DHAVE_CONFIG_H -I. -I/home/raziel/Programacion/Projects/sc_pong/src
-I.. -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -D__LINUX__ -D__PC__ -O0 
-pedantic -Wall -g3 -MT CPhysics.o -MD -MP -MF ".deps/CPhysics.Tpo" -c -o CPhysics.o CPhysics.cpp;
then mv -f ".deps/CPhysics.Tpo" ".deps/CPhysics.Po"; else rm -f ".deps/CPhysics.Tpo"; exit 1; fi
CPlayer.h:132: error: 'Combo' does not name a type
CGame.h:47: error: 'SCREEN_HEIGHT' was not declared in this scope
CGame.h:48: error: 'SCREEN_WIDTH' was not declared in this scope
CGame.h:49: error: 'SCREEN_WIDTH' was not declared in this scope
CGame.h:73: error: 'Ball' was not declared in this scope
CGame.h:73: error: template argument 1 is invalid
CGame.h:73: error: template argument 2 is invalid
CPhysics.cpp: In member function 'void Physics::checkPlayerCollisions(std::vector<Ball*,
std::allocator<Ball*> >*, Player**, int)':
CPhysics.cpp:96: error: 'class Player' has no member named 'combo'
CPhysics.cpp:100: error: 'class Player' has no member named 'combo'
CPhysics.cpp:101: error: 'class Player' has no member named 'combo'
gmake[2]: *** [CPhysics.o] Error 1
gmake[2]: Target `all' not remade because of errors.
gmake[2]: Nothing to be done for `all-am'.
gmake[1]: *** [all-recursive] Error 1
gmake: *** [all] Error 2
*** Exited with status: 2 ***

I can't imagine what's happening, but I think it must be some kind of stupid error. Does anyone know this type of error? Can anyone help me? [Edited by - RazZziel on October 31, 2006 10:39:20 AM]
Skizz
Skizz
Use forward references. Instead of:
//Game *game;Ball(int x, int y, int vx, int vy/*, Game *game*/);

use the following:
class Game *game;Ball(int x, int y, int vx, int vy, class Game *game);

Include "CGame.h" in the CPP and not the H file. In the CPP you don't need to prefix the Game class with 'class'.

Skizz
RazZziel
RazZziel
Thanks for your quick answer, it worked like a charm.

I didn't know about forward refferences in classes, I think I'll must improve my c++ skills :p
Zahlman
Zahlman
0) When you have that much code / error dump, use source boxes instead of code formatting, please.

1 through n-1) Read this. Seriously.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.