2

So, I'm in this situation right now. I have two classes A and B. The B is subclass of A and there's also a global object of the B class which is initialized in the main. A function of the A class uses that global object and calls its functions. In what order do I have to write the declarations in order for the compiler to read everything?

I keep getting the same errors whatever I try. Namely: - (x) does not name a type - invalid use of incomplete type (x) - forward declaration of (x)

Code example:

class B;

B* B_GLOBAL;

class A{
   public:
       void A_function(){
           B_GLOBAL->B_function();
       }
   private:        
};

class B : public A{
   public:
       void B_function();
   private:
};

int main(void){

   B_GLOBAL = new B;

   return 0;
}
Stelios
  • 35
  • 3
  • Does this answer your question? [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) –  Jan 02 '20 at 03:52

1 Answers1

4

Move the definition of A_function below the declaration of B:

class B;

B* B_GLOBAL;

class A{
   public:
       void A_function();
   private:        
};

class B : public A{
   public:
       void B_function();
   private:
};

void A::A_function(){
   B_GLOBAL->B_function();
}

int main(void){

   B_GLOBAL = new B;

   return 0;
}