0

This is my EngineIncludes.h file:

#include <stdafx.h>

//Engine Includes
namespace Engine
{
    //Classes: 23
    class BaseObject;
    class Console;
    class Engine;
    class Node;
    template <class T> class Transform;
}

#include "core/BaseObject.h"
#include "core/Console.h"
#include "core/Engine.h"
#include "math/Transform.h"
#include "scene/Node.h"

//Global Objects
extern Engine::Console*             CONSOLE;
extern Engine::Engine*              CORE_ENGINE;

In stdafx.h I have regular stuff like OpenGL, std::map, boost... and I'm building a precompiled header as standard. This is the Node.h file:

#ifndef _NODE_H
#define _NODE_H
#include <stdafx.h>
#include <EngineIncludes.h>

namespace Engine
{
        class Node : public BaseObject
        {
        public:
            Node();
            ~Node();

            void SetParent(Node* parent);
            Node* GetParent();

            void SetTransform(const Transform<double> &transform);
            Transform<double> GetTransform();
            Transform<double> GetDerivedTransform();
            Transform<double> GetInheritedTransform();

            void Update();

        
        private:
            Transform<float>    transform;
            Transform<float>    derived;
            Node*               parent;
        };
 }

 #endif _NODE_H

I get 3 errors here. One C2504 that Engine::BaseObject is not defined. And two C2079 that both Transform transform and Transform use undefined class. Now this is the BaseObject.h:

 #ifndef _BASE_OBJECT_H
 #define _BASE_OBJECT_H
 #include "stdafx.h"
 #include "EngineIncludes.h"

 namespace Engine
 {
     class BaseObject
     {
     public:
         BaseObject()
         {
             id = CORE_ENGINE->GenerateID();
         }

         ~BaseObject()
         {

         }
         long GetID()
         {
             return id;
         }

         private:
            long id;
         };
     }

 #endif _BASE_OBJECT_H

Now I specifically fully defined BaseObject inside header file with no luck. Still the same error. But if I comment out forward declarations in EngineIncludes.h, the compiler freaks out with

extern Engine::Console*    CONSOLE;

It literally ignores all #includes for whatever reason. I've never had an issue like this before. And I tried everything. I even created EngineIncludes.cpp file. I moved the contents of EngineIncludes.h into stdafx.h with no luck. It somehow just ignores #includes. Even if I put #include "BaseObject.h" inside "Node.h" nothing changes. When it compiles Node it has both Transform which is a template class and BaseObject undefined, even though both objects are clearly defined before Node with forward declarations and #includes.

I'm using Visual Studio 2010, but never had an issue like this. I looked into my previous projects and all is written the same fashion and works without problem.

Puso
  • 263
  • 1
  • 8
  • 1
    Seems like a standard circular include problem. Comment out all `#include` directives, and only add the ones you really need. Solve dependency problems with *forward declarations* and by splitting member functions into separate declarations (in header file) and definitions (in source file). – Some programmer dude Dec 28 '20 at 16:45
  • Also note that all symbols beginning with an underscore and an upper-case letter (like e.g. `_NODE_H`) are reserved everywhere. See [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Some programmer dude Dec 28 '20 at 16:46
  • 2
    You don't normally include the precompiled header from a header only source files. – drescherjm Dec 28 '20 at 16:54
  • Likely the problem is this: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Dec 28 '20 at 16:56
  • Ok, I solved the issue with putting #include on the bottom stdafx.h. But every time I make a small correction in any of header files it rebuilds precompiled header file, and with using boost libraries it's pretty slow. – Puso Dec 28 '20 at 17:28
  • You should not include your own headers in `stdafx.h` unless you plan to never or rarely change the header. The files you should include in `stdafx.h` should be headers from boost and windows and any header that likely won't change. – drescherjm Dec 28 '20 at 17:32
  • I know. But if I remove it from stdafx.h the same issues begin with objects not defined, even though you put that objects header file in the same file as it is used, it just ignores it, and the compiler compiles files at random. Event though you put BaseObject.h the on the top of includes, the compiler compiles Node.cpp first instead of BaseObject.cpp and so the base object is not defined when it is needed, even though you put BaseObject.h into Node.cpp :D – Puso Dec 28 '20 at 17:44
  • If you have a circular include path where your headers form a loop of includes solve that using the link I posted earlier. Make sure the `BaseObject.h` does not include `EngineIncludes.h` or `Node.h`. Your question is not clear on this aspect and that is why you have not got more help. – drescherjm Dec 28 '20 at 17:47
  • 1
    When you remove your project header files from the stdafx.h pre-compiled header file and that causes objects not defined, that seems to be that you are using stdafx.h incorrectly in your source code. There are some rather strict rules about using stdafx.h, such as the include for it MUST appear first in the `*.cpp` file, and it MUST NOT appear in any header file. – Eljay Dec 28 '20 at 18:04
  • Oh I see you do have a circular include path: `BaseObject.h` should not include `stdafx.h` and also should not include `EngineIncludes.h`. You should have a `BaseObject.cpp` (where you can include the headers) and define the constructor in that if you need something from these headers. – drescherjm Dec 28 '20 at 18:21
  • ok after hours I managed to solve. I removed EngineIncludes.h from every header file, and putting it only in *.cpp files where needed. Wherever in C++ code there is CONSOLE (which is defined in EngineIncludes.h) I put it there, otherwise only #includes that are needed. And now the compile time is faster. Thanks for help. :) – Puso Dec 28 '20 at 19:41

0 Answers0