错误:尽管包含头文件,但尚未声明类,并且代码在其他地方编译得很好

error: Class has not been declared despite header inclusion, and the code compiling fine elsewhere(错误:尽管包含头文件,但尚未声明类,并且代码在其他地方编译得很好)

本文介绍了错误:尽管包含头文件,但尚未声明类,并且代码在其他地方编译得很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个类包含在另一个类中,该类不断抛出形式为错误:'问题类'尚未声明的编译错误.文件是这样设置的:

So I have a class included in another class that keeps throwing a compile error of the form "error: 'ProblemClass' has not been declared. The files are set up thusly:

#ifndef PROBLEMCLASS_H
#define PROBLEMCLASS_H

#include <iostream>
#include <cmath>

class ProblemClass
{
  public:

    virtual void Init() = 0;
};

#endif

发生错误的类如下所示:

and the class where the error occurs looks like this:

#ifndef ACLASS_H
#define ACLASS_H

#include "problemclass.h"

class AClass : public Base
{
  public:

    void DoSomething(ProblemClass* problem);

};

#endif

编译错误发生在 void Dosomething();

The compile error occurs at void Dosomething();

我知道这里的代码不足以解决问题.我一直无法创建一个可以重现它的最小示例.所以我的问题要笼统得多;什么样的事情可能会导致这种情况?有什么我应该特别寻找的东西,或者我应该遵循一些查询来追踪它吗?

I'm aware the code here isn't enough to solve the problem. I've been unable to create a minimal example that can reproduce it. So my question is much more general; what sort of things might cause this? Is there anything in particular I should look for, or some line of enquiry I should be following to track it down?

这段代码在几乎相同的项目版本中编译得很好.

This code compiles fine in an almost identical version of the project.

无论多么模糊,任何形式的帮助都将不胜感激.我在 win 7 64 位中使用代码块 10.05 和 mingw4.4.1.

Help of any sort would be greatly appreciated, no matter how vague. I'm using codeblocks 10.05 with mingw4.4.1 in win 7 64 bit.

推荐答案

您似乎是在说您显示的代码实际上并未产生您遇到的编译器错误.所以我们只能猜测.以下是一些可能性:

You seem to be saying that the code you are showing doesn't actually produce the compiler error that you are having a problem with. So we can only guess. Here are some possibilities:

  • 您可能忘记在使用 ProblemClass 的文件中包含 problemclass.h.
  • 您可能在 ProblemClass 自己的头文件中或在您使用它的地方拼错了名称.如果是大写错误,例如编写 ProblemclassproblemClass 而不是 ProblemClass,这可能很难被发现.
  • 您可能将包含保护 #defines 从一个头文件复制粘贴到另一个头文件,然后忘记更改定义的名称.那么只有这两个包含的头文件中的第一个才会生效.
  • 您可以将 ProblemClass 放在命名空间 A 中,在这种情况下,您必须将 ProblemClass 称为 A::ProblemClass 如果您是从名称空间 A 之外引用它.
  • 您可能正在使用模板并且不期望两阶段查找以它确实.
  • 您可能在包含中拼错了文件名.如果您在拼写错误的名称下还有该文件的旧版本,则编译器不会报告错误.
  • 您可以使 ProblemClass 成为一个只有在包含 problemclass.h 后才能定义的宏,在这种情况下,您看到的是 ProblemClass被宏预处理器替换为其他内容.
  • 您可以在头文件中定义 ProblemClass 而不是 problemclass.h,然后 problemclass.h 实际上定义了其他东西.莉>
  • You could have forgot to include problemclass.h from the file where you are using ProblemClass.
  • You could have misspelled the name of ProblemClass either in its own header file or in the place where you are using it. This can be hard to spot if it is a capitalization error such as writing Problemclass or problemClass instead of ProblemClass.
  • You could have copy-pasted your inclusion guard #defines from one header file to another and then forgot to change the defined names. Then only the first of those two included header files would take effect.
  • You could have placed ProblemClass in a namespace A, in which case you must refer to ProblemClass as A::ProblemClass if you are referring to it from outside the namespace A.
  • You may be using templates and not expecting two-phase lookup to work the way it does.
  • You could have misspelled the file name in your include. The compiler would not report an error on that if you also have an old version of that file under the misspelled name.
  • You could have made ProblemClass a macro that only gets defined after you include problemclass.h, in which case what you see as ProblemClass gets replaced by something else by the macro preprocessor.
  • You could have defined ProblemClass in a header file other than problemclass.h and then problemclass.h actually defines something else.

这篇关于错误:尽管包含头文件,但尚未声明类,并且代码在其他地方编译得很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:错误:尽管包含头文件,但尚未声明类,并且代码在其他地方编译得很好