Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with inheritance

Help with inheritance

Scheduled Pinned Locked Moved C / C++ / MFC
helpoop
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sirrius
    wrote on last edited by
    #1

    I am trying to do some simple inheritance with a few classes but keep running into an error at compile time. It is error C2504, 'undefined base class'. What does this mean. My classes are defined, I think they are anyway. This is what it looks like.

    class a{

    ;

    class b : public a{

    };

    I am including the files as well. I am just kind of stumped. Thanks Brian.

    C 1 Reply Last reply
    0
    • S Sirrius

      I am trying to do some simple inheritance with a few classes but keep running into an error at compile time. It is error C2504, 'undefined base class'. What does this mean. My classes are defined, I think they are anyway. This is what it looks like.

      class a{

      ;

      class b : public a{

      };

      I am including the files as well. I am just kind of stumped. Thanks Brian.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      ?Don't ask the same question twice. The above is missing a }, so I assume it's not the actual code. Are you including the header for the base class in the HEADER for the derived class ? Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

      S 1 Reply Last reply
      0
      • C Christian Graus

        ?Don't ask the same question twice. The above is missing a }, so I assume it's not the actual code. Are you including the header for the base class in the HEADER for the derived class ? Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

        S Offline
        S Offline
        Sirrius
        wrote on last edited by
        #3

        Yes, I am including the header files. Here is some of the original code.

        #ifndef ABSTRACTFACTORY_H
        #define ABSTRACTFACTORY_H
        #include "concAbstFactory.h"
        #include "abstractFactory.h"
        #include "productA.h"
        #include "productB.h"

        using namespace std;

        class abstractFactory{
        public:

        abstractFactory(){}
         
        productA makeProductA(){
        	
        	return productA();
        
        }
        productB makeProductB(){
        
        	return productB();
        }
        

        };

        #endif

        #ifndef CONCABSTFACTORY_H
        #define CONCABSTFACTORY_H
        #include "abstractFactory.h"
        #include "productA.h"
        #include "productB.h"

        using namespace std;

        class concAbstFactory : abstractFactory{
        public:

        concAbstFactory(){
        
        	cout << "Building concAbstFactory" << endl;
        }
        

        };

        #endif

        R A 2 Replies Last reply
        0
        • S Sirrius

          Yes, I am including the header files. Here is some of the original code.

          #ifndef ABSTRACTFACTORY_H
          #define ABSTRACTFACTORY_H
          #include "concAbstFactory.h"
          #include "abstractFactory.h"
          #include "productA.h"
          #include "productB.h"

          using namespace std;

          class abstractFactory{
          public:

          abstractFactory(){}
           
          productA makeProductA(){
          	
          	return productA();
          
          }
          productB makeProductB(){
          
          	return productB();
          }
          

          };

          #endif

          #ifndef CONCABSTFACTORY_H
          #define CONCABSTFACTORY_H
          #include "abstractFactory.h"
          #include "productA.h"
          #include "productB.h"

          using namespace std;

          class concAbstFactory : abstractFactory{
          public:

          concAbstFactory(){
          
          	cout << "Building concAbstFactory" << endl;
          }
          

          };

          #endif

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          Sirrius wrote: #include "concAbstFactory.h" There's the culprit. Get rid of that line and the sun will being to shine once more :)

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          S 1 Reply Last reply
          0
          • R Ryan Binns

            Sirrius wrote: #include "concAbstFactory.h" There's the culprit. Get rid of that line and the sun will being to shine once more :)

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            S Offline
            S Offline
            Sirrius
            wrote on last edited by
            #5

            Strange. What was so wrong with that. "concAbstFactory.h" is another class that I am going to need to derive other classes from later. Hmmm. Getting rid of it did help though. Thanks for your eagle eye. Brian.

            R 1 Reply Last reply
            0
            • S Sirrius

              Strange. What was so wrong with that. "concAbstFactory.h" is another class that I am going to need to derive other classes from later. Hmmm. Getting rid of it did help though. Thanks for your eagle eye. Brian.

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #6

              Sirrius wrote: Strange. What was so wrong with that. Ok, say we're trying to compile abstractFactory.h... The compiler starts the pass, and hits the #include "concAbstFactory.h" statement, so opens the file and starts to process it, where it finds the line #include "abstractFactory.h". When it opens this file, it gets caught by your #ifndef (since the file is already included), and skips over the file. Returning to the concAbstFactory.h file, it tries to process the class definition but since it hasn't reaches the declaration of abstractFactory yet, it complains that the base class is not defined, which is correct. So basically, the compiler is seeing the lines in this order:

              /* abstractFactory.h */
              #ifndef ABSTRACTFACTORY_H
              #define ABSTRACTFACTORY_H
              #include "concAbstFactory.h"
              /* concAbstFactory.h */
              #ifndef CONCABSTFACTORY_H
              #define CONCABSTFACTORY_H
              #include "abstractFactory.h
              /* abstractFactory.h */
              #ifndef ABSTRACTFACTORY_H
              #endif // Skips over everything in between since the definition is now true
              /* concAbstFactory.h */
              #include "productA.h"
              #include "productB.h"
              using namespace std;
              class concAbstFactory : abstractFactory

              ...etc...

              See the problem?

              Ryan

              "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

              E 1 Reply Last reply
              0
              • R Ryan Binns

                Sirrius wrote: Strange. What was so wrong with that. Ok, say we're trying to compile abstractFactory.h... The compiler starts the pass, and hits the #include "concAbstFactory.h" statement, so opens the file and starts to process it, where it finds the line #include "abstractFactory.h". When it opens this file, it gets caught by your #ifndef (since the file is already included), and skips over the file. Returning to the concAbstFactory.h file, it tries to process the class definition but since it hasn't reaches the declaration of abstractFactory yet, it complains that the base class is not defined, which is correct. So basically, the compiler is seeing the lines in this order:

                /* abstractFactory.h */
                #ifndef ABSTRACTFACTORY_H
                #define ABSTRACTFACTORY_H
                #include "concAbstFactory.h"
                /* concAbstFactory.h */
                #ifndef CONCABSTFACTORY_H
                #define CONCABSTFACTORY_H
                #include "abstractFactory.h
                /* abstractFactory.h */
                #ifndef ABSTRACTFACTORY_H
                #endif // Skips over everything in between since the definition is now true
                /* concAbstFactory.h */
                #include "productA.h"
                #include "productB.h"
                using namespace std;
                class concAbstFactory : abstractFactory

                ...etc...

                See the problem?

                Ryan

                "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                E Offline
                E Offline
                EricP 1234
                wrote on last edited by
                #7

                Hi Sirrius, The problem was very well described by Ryan. The solution to fix your compilation error is to delete both #include statements. Here is an example of a simple yet correct way to include header files like your are trying to do: File ClassA.h:

                #ifndef CLASSA_H
                #define CLASSA_H
                #include "ClassB.h" // <- Remove this line
                #include "ClassA.h" // <- Remove this line
                class a
                { ... };
                #endif

                File ClassB.h:

                #ifndef CLASSB_H
                #define CLASSB_H
                #include "ClassA.h" // Include definition of base class.
                class b : public a
                { ... };
                #endif

                If you want to derive other classes from concAbstFactory then simply include "concAbstFactory.h" in the header files for those new derived classes. Your base classes should not have any knowledge of your derived classes. Hope this helps. :) Eric

                1 Reply Last reply
                0
                • S Sirrius

                  Yes, I am including the header files. Here is some of the original code.

                  #ifndef ABSTRACTFACTORY_H
                  #define ABSTRACTFACTORY_H
                  #include "concAbstFactory.h"
                  #include "abstractFactory.h"
                  #include "productA.h"
                  #include "productB.h"

                  using namespace std;

                  class abstractFactory{
                  public:

                  abstractFactory(){}
                   
                  productA makeProductA(){
                  	
                  	return productA();
                  
                  }
                  productB makeProductB(){
                  
                  	return productB();
                  }
                  

                  };

                  #endif

                  #ifndef CONCABSTFACTORY_H
                  #define CONCABSTFACTORY_H
                  #include "abstractFactory.h"
                  #include "productA.h"
                  #include "productB.h"

                  using namespace std;

                  class concAbstFactory : abstractFactory{
                  public:

                  concAbstFactory(){
                  
                  	cout << "Building concAbstFactory" << endl;
                  }
                  

                  };

                  #endif

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  In the file that defines class abstractFactory, you're including the file that defines class concAbstFactory: #include "concAbstFactory.h" concAbstFactory is derived from abstractFactory (which hasn't yet been defined at this point), so there's your hiccup. Remove the include statement mentioned above and try again. Including the headers of child classes in their base class should make you nervous.

                  A 1 Reply Last reply
                  0
                  • A Anonymous

                    In the file that defines class abstractFactory, you're including the file that defines class concAbstFactory: #include "concAbstFactory.h" concAbstFactory is derived from abstractFactory (which hasn't yet been defined at this point), so there's your hiccup. Remove the include statement mentioned above and try again. Including the headers of child classes in their base class should make you nervous.

                    A Offline
                    A Offline
                    Anonymous
                    wrote on last edited by
                    #9

                    Please pardon the redundant response. I failed to read further into the thread before replying.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups