C++ brushing-up..., class forward declarations Q?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stws
    New Member
    • Jan 2010
    • 2

    C++ brushing-up..., class forward declarations Q?

    Hi Bytes Community,

    I've been away from C++ programming for some years, and now find me self in need to recompile old code that compiles under g++ 2.96, but not under g++ 3.4.3. I notice that g++ 3.4 changed the parser to be more conforming and thus I now see compile errors like tried illustrated below. I've been googlling around, but so far haven't understood the issue at hand, hoping for hints from someone here.

    Got code like this:

    line n+1: class classA; // forward declaration

    line n+3: typedef templateClassC <classA> classD;

    line n+5: class classB : public classD {
    line n+6 virtual A *ptr2A() {};
    line n+7 }


    gives me compile time errors like:

    line n+5: instantiated from here
    line n+1: error: forward declaration of `struct classA'

    Why it g++ 3.4.3 considering the forward declaration as been of a struct and not a class type (assuming my code in non-conforming, but how to resolve this chicken/egg issue)?

    TIA
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    line n+1: class classA; // forward declaration
    
    line n+3: typedef templateClassC <classA> classD;
    shows a class A as a template argument. To use an A you will need a constructor for A and a forward declaration does not provide that.

    You can use a forward declaration for A only to justify using an A* as an argument somewhere. That is:

    Code:
     typedef templateClassC <classA*> classD;
    would be OK provided there's not somethin in the template that requires the compiler to know about the entire classA.

    Your solution here is to remove the forward referention and #include the classA class declaration instead.

    Comment

    • stws
      New Member
      • Jan 2010
      • 2

      #3
      Thanks and sorry see now I forgot to tell classA references classB as well and thus give the chicken&egg problem. So one class needs to be defined before the other and as said 2.96 been less strict conforming compiled the code. Chicken&eggs still exists even with stricter compiler/parsers I assume, how do one work around such then?

      Did try to work a bit on using include files instead as I read a bit here and on gcc.gnu.org, only no better so far.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        As I said, you have to use a pointer to the class.

        That is, if class A references class B and class B references class A then in each case, the contructor for A must be known by the compiler to compile class B and vice versa for class A. You are now dead.

        You make this scenario work by having one of the classes use a pointer rather than a reference. Using a pointer does not require a constructor. Just knowing the class name is enough for the compiler to allow a pointer to an object of that class. More than that and the compiler will need to see the class declaration.


        Code:
        class A;
        class B
        {
              A*  obj;
              //etc...
        
        };

        Comment

        Working...