The error "Do not name type", but the class pointer already has a declaration forward?

I get 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 the front declaration resolves this error. This answer indicates that including a circular header may cause this. But doesn't the #ifndef and #define expressions ban the inclusion of a circular header?

Is there any other reason I can see this error?

What are some possibilities of the approach that I could use to further determine the nature of this error?

Update: This is pretty weird. I have a Globals.h file, and if I define a new enum in Globals.h , an error appears. Then, if I comment on enum , the error will disappear. Does it make me think that a circular dependency has existed for some time, and adding enum somehow reorders compilation units, thereby exposing a dependency that was not there before?

+4
source share
2 answers

In the end, I'm not sure I fully understand why the error occurred, but this is what I did to fix it, and other information. Perhaps this will help others who come across this issue.

  • For each header file in my project
    • For each #include "..." in the title
      • If there are no class references in #include , delete it.
      • If there are only pointers to the class defined in #include , then replace it with the class ... forward declaration.
      • If there is an instance member of the class defined in #include and it makes sense to use a pointer and select the item on the heap, then change the item to a pointer and replace #include with class .... forward declaration.
  • Go through my Globals.h and move everything that can be deduced from it to more localized and specific header files.
    • In particular, remove the enum that was defined, Globals.h and place it in a more localized header file.

Having done all this, I was able to make a mistake. Oddly enough, enum in Globals.h seems to have been a catalyst for error. Whenever I deleted it from Globals.h , the error Globals.h away. I don’t see how this enum can cause an error, so I think it indirectly led to an error. I still couldn’t find out exactly how and why, but this helped me in this guide when coding in C ++:

Do not put anything in the header file unless it should be there. do not put anything in Globals.h that can be placed in a more localized file. Basically, do your best to reduce the amount of code that is included in the #include directives.

+2
source

The #ifndef header protector does not prevent circular dependencies. It simply prevents multiple inclusions of the same header in the same file.

Looks like a circular addiction to me. This means that you #include header in DataAudit.h, which #include DataAudit.h is either directly or indirectly.

+3
source

Source: https://habr.com/ru/post/1495037/


All Articles