-5
The below is my code design
class D; 
class A {
   D *d;
   class B:public C {
   somefunctions();
   }
}

class C {
    //I need to use class D variables inside Class C
}

Whatever I do inside the Class C for bringing Class D variables, it is throwing Invalid use of Incomplete 'Class D' and Forward declaration of 'class D'. The thing is we can't move the class positions. Need your help, Please give suggestions with examples. Thanks in advance for your help

Paiya
  • 7
  • 3
  • 1
    `Need urgent help` time constraints are your issue, not ours. Attempting to pressure the community will not get you anywhere – UKMonkey Dec 05 '17 at 13:29
  • 2
    You tagged C. C engineers and C++ engineers fight in pubs. Best not pull them together. (C) Ron 2017 – UKMonkey Dec 05 '17 at 13:30

2 Answers2

1

The long and short of your problem revolves around your design being a bit like spaghetti. It goes all over the place, and one can not simply work through 1 class at a time and understand what it should do.

To be able to use any function of class D, or create one, the compiler must know what functions are available to that class and how large it is. For that reason, to be able to get this to compile, the simplest thing to do is migrate the implementation of class C into its own cpp file, and then include the D header at the top of that. If that's not an option, then I think you should just save yourself a load of heartache and redesign sooner rather than later.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
0

A class definition depends on the types of its sub objects. If C has a member of type D, then D has to be defined before C. If D is defined after C, then C must not depend on D.

Types typically don't depend on functions, so if a member function of C depends on D, the solution is simple:

  • Define C
  • Define D
  • Define the member function of C that depends on D
eerorika
  • 232,697
  • 12
  • 197
  • 326