I seem to be misunderstanding something related to build order dependencies in C++. So I have this code, which defines a class that I use as a functionoid (object whose purpose is to substitute for passing a function pointer):
#include "Imports.h"
class X: public Y
{
public:
X (T* t) { this->t= t; }
virtual ~X(){}
virtual void draw()
{
if (t->booleanReturningFunction())
{
t->someField.draw();
}
}
T* t;
};
I’m getting a compiler error that complains about the “use of undefined type T” at the line numbers where I’m using T. However, Imports.h looks like:
//The goal of this file is to have all the typcally needed imports in one place.
#if !defined(IMPORTS_H)
#define IMPORTS_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <assert.h>
#include <cmath>
#include <fstream>
class T;
class X;
class Y;
#include "Y.h"
#include "X.h"
#include "T.h"
#endif // if !defined(IMPORTS_H)
With each “.h” file containing a definition of that class. Now T actually does have an X object in it (not a pointer, but an X object). But, as far as I can tell, there’s no circular dependency in the build order because X only has a pointer to T, right? Is there anything I’m missing that you can see just from this code? Help is much appreciated!
Edit: I solved my problem. The issue was that I was doing the above code inside a header file. The compiler understandably couldn’t compile t->booleanReturningFunction() based on a forward reference (it needed to see the class declaration to know what address to bind the function call to).
I solved my problem. The issue was that I was doing the above code inside a header file. The compiler understandably couldn’t compile t->booleanReturningFunction() based on a forward reference (it needed to see the class declaration to know what address to bind the function call to).