Domanda

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?

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. Then if I comment out the enum, the error goes away. 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?

È stato utile?

Soluzione 2

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
      1. If there are no references to the class in the #include, remove it.
      2. If there are only pointers to the class defined in the #include, then replace it with a class ... forward declaration.
      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.
  2. Go through my Globals.h and move anything that can be moved out of it to more localized and specific header files.
    1. Specifically, remove an enum that was defined is Globals.h, and place it in a more localized header file.

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. Whenever I removed it from Globals.h, the error would go away. I don't see how this enum could cause the error, so I think it indirectly led to the error somehow. I still wasn't able to figure out exactly how or why, but it has helped me for this guideline when coding in 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. Basically, do all you can to reduce the amount of code that is included through the #include directives.

Altri suggerimenti

The #ifndef header guard doesn't prevent circular dependencies. 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 #includes DataAudit.h either directly or indirectly.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top