Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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?

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?

like image 571
Cory Klein Avatar asked Aug 02 '13 22:08

Cory Klein


1 Answers

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.

like image 170
Phlucious Avatar answered Oct 07 '22 00:10

Phlucious