‘{’ 之前不可能有预期的类名令牌错误要解决

发布于 2024-10-05 00:29:06 字数 1549 浏览 5 评论 0原文

这是一个众所周知的问题,这个该死的错误

在“{”标记之前期望类名

好吧,尽管我努力工作和谷歌搜索,但我无法解决这个错误。对不起。这是我最后的岸。

在我的一个项目的 ui.cpp 中,我这样做:

#include "wfqueue_proxy_factory.hpp"

好的,这会在我的编译器中引发这个愚蠢的错误:

包含在文件中 wfqueue_proxy_factory.hpp:29,来自 用户界面.cpp:28: wfqueue_manager_proxy.hpp:42:错误: 预期的类名位于“{”标记之前

我的项目中有三个类: 第一

// wfqueue_proxy_factory.hpp
#ifndef _WFQUEUE_PROXY_FACTORY_HPP
#define _WFQUEUE_PROXY_FACTORY_HPP
#include "wfqueue_manager_proxy.hpp"
// ...
class WFQueueProxyFactory {
//...
};
#endif

第二

// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};
#endif

第三

// wfqueue.hpp
#ifndef _WFQUEUE_HPP
#define _WFQUEUE_HPP
#include "workflow.hpp"
class WFQueue {
// ...
};
#endif

请注意我使用;在每个类的 } 之后,我检查了项目中的每个标头来寻找这个问题,但没有找到任何后面没有 ; 的类。在其右括号之后。这对于工作流.hpp 是有效的,它是一个简单的类(不派生自任何类,只是一个普通类)。

WFQueue 是某种接口,我也将这种模式与其他类一起使用,并且它们可以工作。 WFQueue 包含一些虚拟的纯方法...无论如何,问题不应该在这里...我想这是因为我使用另一个“接口”类与其他类,并且它们工作正常。

如果我这样做,这个错误就会消失:

// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy {
//...
};
#endif

真的不知道如何解决这个问题...请帮助我。 谢谢

It's a well known issue this damn error

expected class-name before ‘{’ token

Well, despite my hard working and googling, I could not solve this error. Sorry. This is my last shore.

In ui.cpp of a project of mine I do:

#include "wfqueue_proxy_factory.hpp"

OK, this raises this stupid error in my compiler:

In file included from
wfqueue_proxy_factory.hpp:29,from
ui.cpp:28:
wfqueue_manager_proxy.hpp:42: error:
expected class-name before ‘{’ token

There are three classes in my project:
First

// wfqueue_proxy_factory.hpp
#ifndef _WFQUEUE_PROXY_FACTORY_HPP
#define _WFQUEUE_PROXY_FACTORY_HPP
#include "wfqueue_manager_proxy.hpp"
// ...
class WFQueueProxyFactory {
//...
};
#endif

Second

// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};
#endif

Third

// wfqueue.hpp
#ifndef _WFQUEUE_HPP
#define _WFQUEUE_HPP
#include "workflow.hpp"
class WFQueue {
// ...
};
#endif

PLEASE PLEASE PLEASE note that I use ; after } of every class, I checked out EVERY header in my project looking for this problem and didn't find any class not followed by ; after its closing bracket. This is valid for workflow.hpp which is a simple class (not deriving from any class, just a plain class).

WFQueue is some sort if interface, I use this pattern with other classes too and they work. WFQueue contains some virtual pure methods... problem should not be here anyway.... I suppose this because I use another "interface" class with other classes and they work fine.

This error disappears if I do this:

// wfqueue_manager_proxy.hpp
#ifndef _WFQUEUE_MANAGER_PROXY_HPP
#define _WFQUEUE_MANAGER_PROXY_HPP
#include "workflow.hpp"
#include "wfqueue.hpp"
// ...
class WFQueueManagerProxy {
//...
};
#endif

Don't really know how to solve this problem... please help me.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

ま柒月 2024-10-12 00:29:06

您应该在代码上运行预处理器但不编译它,并检查结果。为此,请复制运行失败编译的命令,然后对于大多数编译器,您将删除 -o outfile 选项并添加类似 -E 的内容(请参阅您的仅进行预处理的标志的编译器文档)。

编译器将(在标准输出上)发出整个翻译单元,其中包含所有 #include 和此类已解析的内容,因此您可以清楚地看到缺少的内容(只需搜索与错误行匹配的代码行,然后查找以查看您的声明)寻找)。如果仍然不清楚问题所在,请将预处理的输出写入文件并尝试编译该文件。然后,您可以调整预处理后的源代码并查看需要做什么来修复它。

You should run the preprocessor on your code but not compile it, and examine the result. To do this, copy the command which runs the failing compilation, and with most compilers you'd then remove the -o outfile option and add something like -E (see your compiler's documentation for the flag which does preprocessing only).

The compiler will emit (on stdout) the entire translation unit with all #includes and such resolved, so you can clearly see what is missing (just search for the line of code that matches the error line, then look up to see what declarations you find). If it's still not clear what the problem is, write the preprocessed output to a file and try compiling that. You can then tweak the preprocessed source and see what's needed to fix it.

予囚 2024-10-12 00:29:06

只是一个大胆的猜测:您的错误表明在

class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};

{ 之前必须有一个类名。因此我假设编译器不知道 WFQueue 是一个类。您确定包含其定义吗?我的意思是,也许在 wfqueue.hpp 中,该类被命名为 WfQueue 或以其他方式有所不同?

Just a wild guess: Your error says that in

class WFQueueManagerProxy : public WFQueue { // This is the problem (line 42)
//...
};

there must be a class name before {. Therefore I assume that the compiler doesn't know that WFQueue is a class. Are you sure that its definition is included? I mean, maybe in wfqueue.hpp the class is named WfQueue or different in some other way?

迷你仙 2024-10-12 00:29:06

问题可能出在错误的命名上,包括警卫。尝试检查每个文件它们是否确实是唯一的。看来您在编译 WFQueueManagerProxy 时禁用了 WFQueue 的定义。

The problem might be in misnamed include guards. Try to check if they are really unique per file. It seems that you made it to disable the definition of WFQueue while compiling WFQueueManagerProxy.

森林很绿却致人迷途 2024-10-12 00:29:06

这是从未发生过的事情……天哪,对不起……
看来我的虚拟机备份磁盘与原来的磁盘冲突了。我在虚拟机上运行我的项目,进行备份,2小时前,可能搞砸了一些东西......我调整了它,现在虚拟机可以找到正确的文件夹和正确的文件进行编译。这是令人惊奇的啊哈哈,而且很明显,g++ 尝试编译的 ols 文件在以前的版本中充满了错误...这是其中一个错误...重复了一个保护头。
Icecrime是对的......尽管我在我的文件中查找重复项,但在之前的版本中,我没有解决这个问题,我粘贴了一些文件并忘记更改保护头。
感谢大家的耐心和努力。
抱歉,我没有注意到我的机器中出现这种非常奇怪的虚拟磁盘冲突。再次感谢。

It's something it never happened... my god sorry...
It seems that my virtual machine backup disk collided with the original one. I run my project on a virtual machine, making the backup, 2 hours ago, probably messed up something... I adjusted it and now the virtual machine can locate the correct folder and the correct files to compile. It was amazing ahaha and obvious, the ols files g++ tried to compile where a previous version filled with mistakes... This was one of that bugs... a guard header repeated.
Icecrime was right... despite I looked for repetitions in my files, in the previous version, where I didn't fix this problem, there were some files I pasted and forgot to change guard header.
Thank you everyone for your patience and effort.
I'm sorry I didn't notice this very strange virtual disk collision in my machine. Thanks again.

如日中天 2024-10-12 00:29:06

确保您

using namespace omnetpp;

在包含之后键入。它解决了我的问题。

Make sure you typed

using namespace omnetpp;

after includes. It solved my problem.

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