2

I will dive straight into the code. I am getting

"error C2143: syntax error : missing ';' before '*'

and

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Here is how I have my header files defined as I think there is some problem with the header file declaration itself. I am getting the error in my Source.h file on the line where I declare my player object pointer - CPlayer* myplayer

Firstly,

Headers.h

#pragma once

#include <iostream>
#include <string>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>

GameSetup.h

#pragma once
#include "Headers.h"

class CGameSetup
{
public:
    CGameSetup(bool* _running,int ScreenWidth, int ScreenHeight);
    ~CGameSetup(void);

    SDL_Window* GetWindow();
    SDL_Renderer* GetRenderer();

    void Begin();
    void End();

private:
    SDL_Window* _screen;
    SDL_Renderer* _renderer;
};

Source.h

#pragma once
#include "Headers.h"
#include "GameSetup.h"
#include "Sprite.h"
#include <math.h>
#include "Player.h"

class CSource
{
public:
    CSource(int ScreenWidth,int ScreenHeight);
    ~CSource(void);
    void GameLoop();
    CSprite* getPlayerSpriteReference();
    SDL_Event getEventHandler();
    int getMouseX();
    int getMouseY();

private:
    bool _running;
    CPlayer* myplayer;  // THIS IS WHERE I AM GETTING THE TWO ERRORS ON**
    SDL_Event events;
    CGameSetup* gameSetup;
    CSprite* backgroundSprite;  
    int Mouse_X;
    int Mouse_Y;
    void Draw();
    void Update();
};

Player.h

#pragma once
#include "Source.h"

namespace playerStates {
    enum States {down=1,up,left,right,standing};
}

class CPlayer
{
public:
    CPlayer(CGameSetup* gameSetup, int* p_MouseX, int* p_MouseY);
    ~CPlayer(void);
    void playerMove();
    void drawPlayerSprite();
    CSprite* getPlayerSpriteReference();

private:
    CSprite* playerSprite;
    CGameSetup* gSetup;
    SDL_Event events;
    int* Mouse_X;
    int* Mouse_Y;
    int* newMousePos_X;
    int* newMousePos_Y;
    int currentTime;
    bool movePlayer;
    float GetDistance(float x1,float y1,float x2,float y2);
    void SetPlayerState(int type);
};

Those are the 4 header files I have. Could someone please tell me if there is any problem in declared the include header files or are these errors related to something else which I am having a hard time troubleshooting!

Htlcs
  • 545
  • 3
  • 13
  • 26

1 Answers1

4

Whats happening here is your compiler is going into Player.h marking it as "don't go back in here for this parse tree" and then encountering Source.h.

In Source.h you include Player.h but the compiler has already marked Player.h as "do not come back here" so it gets skipped.
When the line CPlayer* myplayer; is hit, the compiler has no idea what CPlayer is and throws a type error because it assumes that it is a name with a default int type.

The parse tree would look like (simplified):

CPlayer.h
 -- Source.h
  -- Cplayer.h -- Skipped, #pragma once

Resolving the circular dependency will fix the problem for you.

Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • So what I did was I changed my player.h by removing source.h and adding gamesetup.h and sprite.h. That resolved the issue. How else could I have solved this issue? – Htlcs Jun 10 '14 at 04:32
  • 3
    @JimZilla you can use *forward declarations*. Since `Source.h` actually doesn't need to know anything about `CPlayer` other than the fact that it exists and it is a `class`, you could write: `class CPlayer;` in `Source.h` to announce this info. – M.M Jun 10 '14 at 04:36
  • 1
    You did the right thing. Re factoring your code until there is no cyclic dependency is the only way to get rid of them. As Matt says forward declarations help a lot too. – Serdalis Jun 10 '14 at 04:37