简体   繁体   English

“不命名类型”错误,但类指针已具有前向声明?

[英]“Does not name a type” error, but class pointer already has forward declaration?

I am getting this compiler error 我收到此编译器错误

error: 'RawLog' does not name a type

Here is the relevant code: 以下是相关代码:

//DataAudit.h
#ifndef DATAAUDIT_H
#define DATAAUDIT_H

class RawLog;

class DataAudit
{
...
private:
    RawLog* _createNewRawLog(); // This is the line indicated with the error
};

#endif // DATAAUDIT_H

Usually a forward declaration resolves this kind of error. 通常,前向声明可解决此类错误。 This answer indicates that a circular header inclusion may cause this. 答案表明循环头包含可能导致此问题。 But doesn't the use of the #ifndef and #define statements prevent circular header inclusion? 但是,使用#ifndef#define语句是否可以防止循环头包含?

Is there another reason I might see this error? 还有其他原因可能会导致我看到此错误吗?

What are some avenues of approach I could use to further deduce the nature of this error? 我可以使用哪些方法来进一步推论此错误的性质?

Update: This is rather odd. 更新:这很奇怪。 I have a Globals.h file, and if I define a new enum in Globals.h , the error appears. 我有一个Globals.h文件,如果在Globals.h定义一个新的enum ,则会出现错误。 Then if I comment out the enum , the error goes away. 然后,如果我注释掉enum ,错误就会消失。 This leads me to think that the circular dependency has existed for a while, and adding the enum somehow re-orders the compilation units, thus exposing the dependency that wasn't there before? 这使我认为循环依赖已经存在了一段时间,并以某种方式添加enum对编译单元进行重新排序,从而暴露了以前不存在的依赖?

The #ifndef header guard doesn't prevent circular dependencies. #ifndef标头防护不会阻止循环依赖关系。 It just prevents multiple inclusions of the same header in a single file. 它只是防止在单个文件中包含多个相同的标头。

Looks like a circular dependency to me. 看起来对我来说是循环依赖。 This means you #include a header in DataAudit.h that #include s DataAudit.h either directly or indirectly. 这意味着你#include在DataAudit.h一个报头#include小号DataAudit.h直接或间接的影响。

In the end, I am not sure I understand completely why the error occurred, but this is what I did to resolve it, and some other related information. 最后,我不确定我是否完全理解错误发生的原因,但这是我为解决该错误所做的工作,以及其他一些相关信息。 Maybe this will help others that come across this question. 也许这会帮助遇到此问题的其他人。

  1. For each header file in my project 对于我项目中的每个头文件
    1. For each #include "..." in the header 对于标题中的每个#include "..."
      1. If there are no references to the class in the #include , remove it. 如果#include中没有对该类的引用,请将其删除。
      2. If there are only pointers to the class defined in the #include , then replace it with a class ... forward declaration. 如果仅包含指向#include定义的类的指针,则将其替换为class ... forward声明。
      3. If there is a member instance of the class defined in #include and it makes sense to use a pointer and allocate the member on the heap, then change the member to a pointer, and replace the #include with a class .... forward declaration. 如果在#include定义了该类的成员实例,则可以使用指针并在堆上分配该成员,然后将该成员更改为指针,并将#include替换为class ....宣言。
  2. Go through my Globals.h and move anything that can be moved out of it to more localized and specific header files. 浏览我的Globals.h并将所有可以移出的内容移动到更本地化和特定的头文件中。
    1. Specifically, remove an enum that was defined is Globals.h , and place it in a more localized header file. 具体来说,删除定义为Globals.henum ,并将其放置在本地化的头文件中。

After doing all this I was able to make the error go away. 完成所有这些操作后,我能够使错误消失。 Strangely enough, the enum in Globals.h seemed to be a catalyst for the error. 奇怪的是, Globals.henum似乎是错误的催化剂。 Whenever I removed it from Globals.h , the error would go away. 每当我从Globals.h删除它时,该错误就会消失。 I don't see how this enum could cause the error, so I think it indirectly led to the error somehow. 我看不到该enum如何导致错误,因此我认为它以某种方式间接导致了错误。 I still wasn't able to figure out exactly how or why, but it has helped me for this guideline when coding in C++: 我仍然无法弄清楚确切的方式或原因,但是当我用C ++编码时,它对我的​​指导方针有帮助:

Don't put anything in a header file unless it needs to be there. 除非需要在头文件中放置任何内容,否则不要在其中放置任何内容。 Don't place anything in a Globals.h that can be placed in a more localized file. 不要在Globals.h中放置任何可以放置在更本地化的文件中的东西。 Basically, do all you can to reduce the amount of code that is included through the #include directives. 基本上,您可以尽一切努力减少通过#include指令包含的代码量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM