c++oopgcccodelite

Top level declarator error but the program runs correctly


I have a project with multiple files including main.cpp and two headers. Both headers have an error at the line with class name declaration. Building any of the files or project as a whole gives no errors or warnings. The program itself runs correctly.

I'm using CodeLite IDE and GCC compiler.

What can be a reason for such behaviour and could it lead to any issues in the future?

#include <Creature.h>
#include <Party.h>

int main() { 
    // Does something with the stuff from header files.
    return 0;
}

Inside Creature.h:

#pragma once

class Creature { // Error: expected ';' after top level declarator
    // something
};

Inside Party.h:

#pragma once

class Party { // Error: expected identifier or '('
    // something
};

Solution

  • Your IDE thinks that the header files are written in C (where class is not a keyword and so Creature is a declarator), because you gave them the conventional extension .h used to indicate that. Don't do that: use .hh, .hpp, or .hxx for C++ header files so that tools (and humans) know what you're writing without having to understand the file.