头文件错误:预期为 '='、'、'、';'、'asm'或“__属性__”前

发布于 2024-12-11 09:27:14 字数 301 浏览 0 评论 0原文

我目前正在运行一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-12-18 09:27:14

这是当您尝试使用 C 编译器编译 C++ 代码时出现的错误。

pax$ cat qq.cpp
class Relation {
public:
    Relation();
    virtual ~Relation();
};

pax$ g++ -c -o qq.o qq.cpp

pax$ cp qq.cpp qq.c

pax$ gcc -c -o qq.o qq.c
qq.c:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Relation'

您应该检查您的 cc 编译器实际上能够编译 C++,以及需要哪些选项(如果有)来实现此目的。

如果它是 gcc(基于完全相同的错误消息,它确实看起来像这样),您可能需要确保您正在调用 g++ 而不是 gcc 和/或源文件(不是标头)的扩展名是可识别的,例如 .cpp (a)< /sup>。

我不完全确定gcc遵循的规则,但我总是发现使用blahblah.cpp和< em>明确使用g++


(a) 推理:既然您在特定情况下提到您正在使用正确的标头文件扩展名,我认为一种可能性是标头扩展名没有任何效果关于 gcc 尝试将源文件编译为什么。它仅使用 source 文件扩展名,具体如下:

pax$ cat xyzzy.hpp
class Relation {
public:
    Relation();
    virtual ~Relation();
};

pax$ cat plugh.c
#include "xyzzy.hpp"

pax$ gcc -c -o plugh.o plugh.c
In file included from plugh.c:1:
xyzzy.hpp:1: error: expected '=', ',', ';', 'asm' or '__attribute__'
                    before 'Relation'

pax$ cp plugh.c plugh.cpp

pax$ gcc -c -o plugh.o plugh.cpp

换句话说,我认为没有包含(例如)xyzzy.hpp 的头文件如果包含 C++ 的源文件仍然是 plugh.c,则将强制编译器编译 C++。

That's the error you get when you try to compile C++ code with the C compiler.

pax$ cat qq.cpp
class Relation {
public:
    Relation();
    virtual ~Relation();
};

pax$ g++ -c -o qq.o qq.cpp

pax$ cp qq.cpp qq.c

pax$ gcc -c -o qq.o qq.c
qq.c:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Relation'

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 calling g++ rather than gcc 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 like blahblah.cpp and explicitly use g++.


(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:

pax$ cat xyzzy.hpp
class Relation {
public:
    Relation();
    virtual ~Relation();
};

pax$ cat plugh.c
#include "xyzzy.hpp"

pax$ gcc -c -o plugh.o plugh.c
In file included from plugh.c:1:
xyzzy.hpp:1: error: expected '=', ',', ';', 'asm' or '__attribute__'
                    before 'Relation'

pax$ cp plugh.c plugh.cpp

pax$ gcc -c -o plugh.o plugh.cpp

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 still plugh.c.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文