错误:尽管头包含,类尚未声明,代码在其他地方编译 [英] error: Class has not been declared despite header inclusion, and the code compiling fine elsewhere

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

问题描述

所以我有一个类包含在另一个类,不断抛出一个编译错误的形式错误:'ProblemClass'没有被声明。文件设置如此:

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位使用带有mingw4.4.1的代码块10.05。

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.

推荐答案

你似乎在说,显示实际上不会产生编译器错误,你有一个问题。所以我们只能猜测。这里有一些可能性:

  • 您可能忘记了在使用ProblemClass的文件中包含problemclass.h。
  • 您可能在自己的头文件中或在使用ProblemClass的地方拼写了ProblemClass的名称。这可能很难发现,如果它是大写错误,如写问题类或problemClass而不是ProblemClass。
  • 您可以将包含保护程序#defines从一个头文件复制粘贴到另一个头文件,然后忘记更改定义的名称。然后只有那两个包含的头文件中的第一个将生效。
  • 你可以在命名空间A中放置ProblemClass,在这种情况下,你必须将ProblemClass引用为A :: ProblemClass,如果你从命名空间A外面引用它。
  • You可能正在使用模板,并且不希望两相查找的工作方式
  • 您可以拼写您的包含中的文件名。如果您还有拼写错误名称下的旧版本的文件,编译器不会报告错误。
  • 你可以使ProblemClass成为一个只有在你包含problemclass.h之后才定义的宏,在这种情况下,你看到的ProblemClass被宏预处理器替换为别的东西。
  • 你可以在一个头文件中定义ProblemClass而不是problemclass.h,然后problemclass.h实际上定义了别的东西。

    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:

  • 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.

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

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆