0

Somehow I'm just keep getting this error:

player.cpp(6): error C2143: syntax error : missing ';' before '.'

Here's the header file

    #ifndef Enemy
    #define Enemy
    #include "SDLGameObject.h"
    class Enemy : public SDLGameObject

    {
      public:
         Enemy(const LoaderParams* pParams): SDLGameObject(pParams) {}
         void draw();
         void update();
         void clean() {}
    };
    #endif

$Here's the Enemy.cpp file:

    #include "Enemy.h"
    void Enemy::draw()
    {
       SDLGameObject.draw();
    }

    void Enemy::update()
    {
       m_x -= 1;
       m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
    }

$Here's the SDLGameObject.h that is the base class of Enemy

    #ifndef __SDLGameObject__
    #define __SDLGameObject__
    #include "GameObject.h"
    #include <string>

    class SDLGameObject : public GameObject
    {
      public:
         SDLGameObject(const LoaderParams* pParams);

         virtual void draw();

         virtual void update();
         virtual void clean();

      protected:
         int m_x;
         int m_y;

         int m_width;
         int m_height;

         int m_currentRow;
         int m_currentFrame;

         std::string m_textureID;
     };

     #endif

And GameObject.h is just the abstract base class of SDLGameObject.h

I'm not sure whether to include it, but it seems the error lies in the Enemy.cpp file.

I'd really appreciate for your help since I'm pretty new at this. Thanks everyone :)))

Dai Dao
  • 11
  • 2
  • You've defined `Enemy` as nothing. How is your class going to work? In the other file, you use a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Aug 05 '14 at 14:20
  • 3
    You `#define Enemy` and then you use `Enemy`. Try to `#define ENEMY_H` or some other name you don't want to use. – juanchopanza Aug 05 '14 at 14:20
  • Depending on your compiler, you may be able to replace header guards with `#pragma once`. Depending if you care about other compilers, as it's not universally supported. – Neil Kirk Aug 05 '14 at 14:24

1 Answers1

2

Once the preprocessor has done its work, the compiler will get as its input the entire contents of SDLGameObject.h and the line

`class : public SDLGameObject`

followed by all the other bits. This is because the preprocessor has replaced Enemy with blank text, due to your line #define Enemy. That's why your compiler is issuing a somewhat spurious error.

To remedy, use

#ifndef Enemy_h
#define Enemy_h

instead.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Also, consider using all-uppercase for the #define header guard -- e.g., #ifndef ENEMY_H. This helps avoid name clashes with all mixed-case identifiers. – Andy Thomas Aug 05 '14 at 14:30