error: Class has not been declared despite header inclusion, and the code compiling fine elsewhere(错误:尽管包含标头,但尚未声明类,并且代码在其他地方编译良好)
问题描述
所以我有一个包含在另一个类中的类,它不断抛出形式为错误:'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 位中使用代码块 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
自己的头文件或您使用它的地方拼错了名称.如果是大写错误,例如写ProblemClass
或problemClass
而不是ProblemClass
,这可能很难发现. - 您可能已经将包含保护
#defines
从一个头文件复制粘贴到另一个头文件,然后忘记更改定义的名称.那么只有这两个包含的头文件中的第一个才会生效. - 您可以将
ProblemClass
放在命名空间A
中,在这种情况下,您必须将ProblemClass
称为A::ProblemClass
如果您是从命名空间A
之外引用它. - 您可能正在使用模板并且不希望两阶段查找以 它的方式工作.
- 您可能在包含中拼错了文件名.如果您在拼写错误的名称下还有该文件的旧版本,编译器不会报告错误.
- 您可以将
ProblemClass
设为仅在包含problemclass.h
后才定义的宏,在这种情况下,您将看到ProblemClass
被宏预处理器替换为其他东西. - 您可以在
problemclass.h
以外的头文件中定义ProblemClass
,然后problemclass.h
实际上定义了其他内容.李>
- You could have forgot to include
problemclass.h
from the file where you are usingProblemClass
. - 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 writingProblemclass
orproblemClass
instead ofProblemClass
. - 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 namespaceA
, in which case you must refer toProblemClass
asA::ProblemClass
if you are referring to it from outside the namespaceA
. - 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 includeproblemclass.h
, in which case what you see asProblemClass
gets replaced by something else by the macro preprocessor. - You could have defined
ProblemClass
in a header file other thanproblemclass.h
and thenproblemclass.h
actually defines something else.
这篇关于错误:尽管包含标头,但尚未声明类,并且代码在其他地方编译良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误:尽管包含标头,但尚未声明类,并且代码在其他地方编译良好
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01