问题描述
我有一个包含多个文件的项目,其中包括main.cpp和两个标头。这两个标头在类名声明的行中都有错误。整体上构建任何文件或项目均不会产生任何错误或警告。该程序本身可以正常运行。
我正在使用CodeLite IDE和GCC编译器。
这种行为的原因可能是什么,将来会导致任何问题吗?
#include <Creature.h>
#include <Party.h>
int main() {
// Does something with the stuff from header files.
return 0;
}
内部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
};
解决方法
您的IDE认为头文件是用C编写的(其中class
不是关键字,因此Creature
是声明符),因为您给了它们常规扩展名 {{1 }} 表示这一点。请勿这样做:对C ++头文件使用.h
,.hh
或.hpp
,以便工具(和人员)无需了解文件即可知道您在写什么。 / p>