24

So I have been having this extremely frustrating problem lately with Visual C++ 2012. Up until a few hours ago, I was writing code just fine and everything was working as intended, until I decided to optimize some things and deleted a few classes. I fixed all of the errors that were popping up because of that, e.g. false includes, etc. Unfortunately, after this the VS compiler went crazy. It started giving me errors such as:

Error 14 error C2653: 'Class' : is not a class or namespace name

or even

Error 5 error C2143: syntax error : missing ';' before '}'
Error 4 error C2059: syntax error : '>'

I've checked multiple times, and everything is in it's right place: all headers included, all symbols placed where they should be.

As far as I understand, the problem is not with my code but with the compiler itself... Visual Studio can be really annoying at times, I guess. Anyway, I would really be grateful if someone could help me out on this one.

(By the way, disabling precompiled headers did not work)

Relevant parts of code:

Error 14:

#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(void) {} // This line causes the error

Error 5:

class GameScreen : public BaseScreen
{
public:
    ...
private:
    ...
}; // This line causes the error

Error 4:

private:
     std::vector<BaseEntity*> _EntityList; // This line causes the error

Whole PlayerEntity.h file:

#ifndef PENTITY_H
#define PENTITY_H

#include "BaseEntity.h"

class PlayerEntity : public BaseEntity
{
public:
    PlayerEntity(void);
    PlayerEntity(float, float);
    virtual ~PlayerEntity(void);

    void render(sf::RenderWindow&);
    void update();
private:
    void init();
};

#endif

Whole GameScreen.h file:

#ifndef GSCREEN_H
#define GSCREEN_H

#include "BaseScreen.h"
#include "BaseEntity.h"
#include "PlayerEntity.h"

class GameScreen : public BaseScreen
{
public:
    GameScreen(sf::Vector2u&);
    virtual ~GameScreen(void);

    void start();
    void stop();

    void render(sf::RenderWindow&);
    void update(void);

    void addEntity(BaseEntity*);
    void destoryEntity(int id);
private:
    std::vector<BaseEntity*> _EntityList;
    sf::Vector2u _ScreenDimensions;
};

#endif

Whole BaseEntity.h file:

#ifndef BSENTITY_H
#define BSENTITY_H

#include "Input.h"
#include <SFML/Graphics.hpp>

class BaseEntity
{
public:
    BaseEntity(void);
    virtual ~BaseEntity(void);

    sf::Vector2f position;

    virtual void update(void);
    virtual void render(sf::RenderWindow&);
    void compare(BaseEntity*);
protected:
    sf::Texture *_EntityTexture;
    sf::Sprite  _EntitySprite;

    bool _isAlive;
    int  _id; 

    virtual void init();
};

#endif

Whole Input.h file:

#ifndef INPUT_H
#define INPUT_H

#include "ScreenSystem.h"
#include <SFML/Window.hpp>

class Input
{
public:
    Input(void);
    Input(sf::RenderWindow*);
    virtual ~Input(void);

    static bool keyPressed(int);
    static bool keyReleased(int);

    static bool mouseHeld(int);
    static bool mouseReleased(int);
private:
    static sf::RenderWindow *_Window;
};

#endif

Whole ScreenSystem.h file:

#ifndef GHANDLER_H
#define GHANDLER_H

#include "BaseScreen.h"
#include "MenuScreen.h"
#include "GameScreen.h"
#include <SFML/Window.hpp>

class ScreenSystem
{
public:
    ScreenSystem(void);
    ScreenSystem(sf::RenderWindow*);
    virtual ~ScreenSystem(void);

    BaseScreen *getCurrentScreen(void);
    void setScreen(int);
private:
    int _currentScreenID;

    std::vector<BaseScreen*> _Screens;
    sf::RenderWindow *_Window;
};

#endif
valtari
  • 347
  • 1
  • 3
  • 10
  • 4
    I doubt it's a problem with the compiler itself. – Joseph Mansfield Apr 01 '13 at 09:35
  • Check the closures of your class declarations to ensure they end with `};` and not just `}`; **all of them**. Also check the balance of each and every `{`, `}` pair. Since clearly the content of the PlayerEntity constructor you provided *should* be valid C++, and since the only thing above it is a header include, what possibly makes you think that include file is **not** relevant and therefore should not be included here for examination? – WhozCraig Apr 01 '13 at 09:35
  • 2
    Please provide a [short, self contained, correct example](http://sscce.org/) – Oswald Apr 01 '13 at 09:37
  • @sftrabbit It could be a problem with solution settings in VS; I'm not really sure. – valtari Apr 01 '13 at 09:38
  • @WhozCraig already did that, it's all false errors. None of them exist in the actual code. – valtari Apr 01 '13 at 09:40
  • *"its all false errors"*. Post PlayerEntity.h *in its entirety* as part of your question. – WhozCraig Apr 01 '13 at 09:42
  • "the VS compiler went crazy" & "Visual Studio can be really annoying at times" & "it's all false errors" etc => sorry to disappoint you, but as sftrabbit already said, in 99.9% of the times that's because you are wrong, not the IDE. Also you shouldn't include sentiments like that in your question, it's not useful for anyone. – stijn Apr 01 '13 at 09:43
  • @WhozCraig Edited the question post – valtari Apr 01 '13 at 09:44
  • BaseEntity seems to be puking in your last reported error as well, and I'm guessing it is used/referenced at least once in the `...` sections of your omitted code.. Paste that one in as well, again, verbatim. Sooner or later. You'll find it. – WhozCraig Apr 01 '13 at 09:45
  • @WhozCraig edited once again – valtari Apr 01 '13 at 09:49
  • Yeah as I suspected, GameScreen.h includes BaseEntity class references as well. The anticipation of seeing that header file is downright exciting at this point. I hope it lasts (not). As requested before, BaseEntity.h header file please (and this may go all the way to BaseScreen.h, but we'll see). And I'm assuming you have the std lib headers (, etc... ) in stdafx.h. You should still include the ones used here as a matter of diligence, but thats not likely related to this issue (we hope). – WhozCraig Apr 01 '13 at 09:51
  • @WhozCraig aand once again. damn, this post is getting pretty long – valtari Apr 01 '13 at 09:56
  • This is all things to be looked at when diagnosing this on your side. I have to assume `SFML/Graphics.hpp` is correct and that you made **no** modifications to their code on-purpose or by-accident. Care to guess the next file (if you're not already posting it I'm sorely disappointed) : `Input.h` – WhozCraig Apr 01 '13 at 09:59
  • And for what its worth, apart from relying on the related source files to include stdafx.h and therefore pull in the stdlib headers for you being a bad habit (see prior comment), the only thing that I see so far I would definitely change is all those underscore-leading ids. Their usage is reserved for the implementation, and you should not have ids named like that. Just a side note hopefully-not related. Continuing on... – WhozCraig Apr 01 '13 at 10:01
  • @WhozCraig thanks for your advice, I'll look into to it. updated the main post again; and by the way, I already checked all of the .h and .cpp files for possible causes.. just saying. – valtari Apr 01 '13 at 10:03
  • 4
    As a general piece of advice: Always fix the *first* error you get, not some errors that result from the first one. The first error might confuse the compiler, all following errors might be bogus. Forget error 4, 5 and 14 as long as you haven't fixed error 1. – Daniel Frey Apr 01 '13 at 10:05
  • @DanielFrey Error #1 is about the same as Error #5 and #4. – valtari Apr 01 '13 at 10:06
  • @Jakobson: So which is error #1, and exactly which line does it refer to? What's in `"ScreenSystem.h"` (included from `"Input.h"`)? Why is that header included at all, when there's apparently no dependency on it? And why is `"Input.h"` included from `"BaseEntity.h"` in the first place? – Mike Seymour Apr 01 '13 at 10:08
  • At this point I'm half tempted to just have you pick a failing source file, drill into the C/C++ section of that file, turn off pch, then set the Preprocessor option to "preprocess to a file". Once that is done, copy the preprocessed data (it will be *huge* into your .cpp file, then recompile just that file. There is definitely something whack in your chain somewhere. – WhozCraig Apr 01 '13 at 10:09
  • Ask `cl.exe` to write the preprocessed output to a file (`/P output-file`), then go inspect the file and see if it really matches how you think it should look. (This also means: are all the classes referenced on the problematic lines actually previously declared?) – jamesdlin Apr 01 '13 at 10:10
  • Looking at the preprocessor output as @WhozCraig suggested is definitely the next step. Also, fix the [illegal identifiers](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) in your code. – Daniel Frey Apr 01 '13 at 10:11
  • @MikeSeymour Error #1 refers to a ";" missing before a "}", but it's there. It's in the ScreenSystem.h file, let me post that one. Also, BaseEntity.h shouldn't include Input, thanks for pointing that out – valtari Apr 01 '13 at 10:14
  • To turn on preprocessor file generation in the IDE, select your file (PlayerEntity.cpp seems a good candidate) open Properties, goto Preprocessor, and turn **on** Preprocess to File, Preprocess suppress line numbers, and Keep comments. The manually recompile the .cpp (ctrlF7 if it is the active source file). This will make a *sizable* .i file (PlayerEntity.i if that was your choice) in your intermediate build folder. From there you can copy/paste that monster into your source file at the top, and comment out everything in the original source file from there to bottom. Then recomp that file – WhozCraig Apr 01 '13 at 10:15
  • 2
    @Jakobson: Errors that refer to missing `;` very often are a symptom of the compiler encountering an unrecognized token (like, say, a class name for a class that hasn't been declared yet...). – jamesdlin Apr 01 '13 at 10:18
  • A shot in the dark: Are you just missing `#include `? – Daniel Frey Apr 01 '13 at 10:21
  • @DanielFrey Nope, it's there – valtari Apr 01 '13 at 10:22

2 Answers2

55

You have a circular dependency in your headers. BaseEntity.h includes Input.h, which includes ScreenSystem.h, which includes GameScreen.h, which in turn re-includes BaseEntity.h. This leads to class names appearing before they are declared, causing compilation failure.

To avoid this, do not include headers unnecessarily. For example, do not include Input.h from BaseEntity.h, since it's not needed at all; and do not include BaseScreen.h from ScreenSystem.h since only a declaration class BaseScreen; is needed, not the complete class definition.

Also, check that you do not have duplicate header guards. Some of them do not match the header name (e.g. GHANDLER_H for ScreenSystem.h), which makes me think that they may have been accidentally copied from other headers. Finally, don't use reserved names like _EntitySprite for your own symbols; for simplicity, avoid leading or double underscores.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
3

Did you copy the error messages into your question or did you retype them? Because error 14 has 'Class' with a capital C which is almost certainly not right.

Also, you should use as few include directives in your header files as possible. For example, GameScreen doesn't use PlayerEntity, so you can remove that include and BaseEntity is only used via pointer so you can replace

#include "BaseEntity.h"

with a forward declaration

class BaseEntity;
IronMensan
  • 6,761
  • 1
  • 26
  • 35