头文件错误:预期为 '='、'、'、';'、'asm'或“__属性__”前
我目前正在运行一个 C++ 项目,其中有一个名为 Relation 的类。
我正在尝试编译它,但无论我使用的文件扩展名如何,我都会不断收到此错误。
编译器是 cc,我尝试编译的类,即使如下所示为空,也会导致此错误。
我已经尝试过 C++ 标头扩展,并且出现相同的错误。
#ifndef RELATION_H_
#define RELATION_H_
class Relation {
public:
Relation();
virtual ~Relation();
};
#endif
I'm currently running a c++ project, with a class called Relation.
I'm trying to compile it, but I constantly get this error, regardless of the file extension that I'm using.
Compiler is cc, and the class I'm trying to compile, even when empty as below, causes this error.
I've tried the c++ header extensions, and the same errors occur.
#ifndef RELATION_H_
#define RELATION_H_
class Relation {
public:
Relation();
virtual ~Relation();
};
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是当您尝试使用 C 编译器编译 C++ 代码时出现的错误。
您应该检查您的
cc
编译器实际上能够编译 C++,以及需要哪些选项(如果有)来实现此目的。如果它是 gcc(基于完全相同的错误消息,它确实看起来像这样),您可能需要确保您正在调用 g++ 而不是
gcc
和/或源文件(不是标头)的扩展名是可识别的,例如.cpp
(a)< /sup>。我不完全确定
gcc
遵循的规则,但我总是发现使用blahblah.cpp
和< em>明确使用g++
。(a) 推理:既然您在特定情况下提到您正在使用正确的标头文件扩展名,我认为一种可能性是标头扩展名没有任何效果关于 gcc 尝试将源文件编译为什么。它仅使用 source 文件扩展名,具体如下:
换句话说,我认为没有包含(例如)
xyzzy.hpp
的头文件如果包含 C++ 的源文件仍然是plugh.c
,则将强制编译器编译 C++。That's the error you get when you try to compile C++ code with the C compiler.
You should check that your
cc
compiler is actually capable of compiling C++ and what, if any, options are needed to make it do so.If it's
gcc
(and it certainly looks like it is, based on the absolutely identical error message), you may need to make sure that you're callingg++
rather thangcc
and/or that your extension for the source file (not header) is a recognised one, like.cpp
(a).I'm not entirely certain the rules that
gcc
follows but I've always found it safer to use source files likeblahblah.cpp
and explicitly useg++
.(a) Reasoning: since you mention in your particular case that you're using the correct header file extensions, I think one possibility is that the header extension has no effect whatsoever on what
gcc
tries to compile the source file as. It uses only the source file extension, as per the following transcript:In other words, I don't think having an included header file of (for example)
xyzzy.hpp
would force the compiler to compile C++ if the source file that's including it is stillplugh.c
.