Search code examples
c++visual-c++allegro5

C++ Weird error. Fails to compile


I have been using the allegro 5 libraries for developing a game in C++ for some time. Today I got some weird error:

I have a class called level. I have a header file called levelhandler. Here's how it looks:

#pragma once
#include "level.h"

level level_1;

level *currentlevel;

void initialize_levels()
{

   currentlevel = &level_1;

}

When I try to compile it gives me strange errors like:

error C2086: 'int level' redefinition
error C2143: syntax error : missing ; before 'level_1'

I remember that it could compile before, and I did use currentlevel->Player.X a lot of times, but now I have a lot of that and it gives errors like these:

error C2227: left of '->Player' must point to a class/struct/generic type
error C4430: missing type specifier - int assumed

header pasted from comment

#pragma once
#include "entity.h"
// some more includes
class level {
public:
    enum Tileset { ... };
    enum Tile { ... };
    int tiles[200][200];
    player Player;
    level(void);
    ~level(void);
};

Solution

  • Such errors are hard to find as long as you look at the "Error List" pane. Select View/Output to show the "Output" view. The line after the error C2086 shows the original definition of level.

    You fill find an

    int level;
    

    there as the C2086 tells you. If it's the line

    level level_1;
    

    of your fist example you will have to check the last header file include in your compilation unit. It might end with an int or it has a unbalanced #if clause.

    To find the exact location start using a Short, Self Contained, Correct (Compilable), Example. This helps you to find the bug and saves time of other with their crystal balls.

    Edit:

    Another way to find the reason for this unexpected behavior is to see the preprocessor output. Set the Generate Preprocessed File option int the C/C++/Preprocessor project property page to With line numbers (/P) and look in the generated <sourcefile>.i

    Check that that level.h file has included what you intended.