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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. forward deklaration

forward deklaration

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 3 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.
  • Z Offline
    Z Offline
    zqueezy
    wrote on last edited by
    #1

    hey guys, I've got a little problem... I have two classes. One is the member of the other. but the other still needs a pointer to the first. // A.h #include "B.h" class A; class A { public: B m_BClass; }; // B.h #include "A.h" class B; class B { public: A* m_pAClass; // <-- error here }; somehow this doesn't work... He gives me: "Error 4 Error C2143: Syntaxerror: missing ';' before '*'" cause he doesn't know the class A. Though I forward declarated it. A friend of mine told me about an "inclusion deadlock". but I cannot figure out how to solve it. i need to to a global class with all necessary includes. any hints or links? thx in advance PS: should I put the forward deklaration before the "#include" ???

    D B 2 Replies Last reply
    0
    • Z zqueezy

      hey guys, I've got a little problem... I have two classes. One is the member of the other. but the other still needs a pointer to the first. // A.h #include "B.h" class A; class A { public: B m_BClass; }; // B.h #include "A.h" class B; class B { public: A* m_pAClass; // <-- error here }; somehow this doesn't work... He gives me: "Error 4 Error C2143: Syntaxerror: missing ';' before '*'" cause he doesn't know the class A. Though I forward declarated it. A friend of mine told me about an "inclusion deadlock". but I cannot figure out how to solve it. i need to to a global class with all necessary includes. any hints or links? thx in advance PS: should I put the forward deklaration before the "#include" ???

      D Offline
      D Offline
      DevMentor org
      wrote on last edited by
      #2

      your problem is you got to forward declare class A is the class B header...this is the only way class B will know about class A, but when using a pointer in class B for class A, you do not have to forward declare, because you are already including the header files for class A inside class B!!! try taking about the forward declare in both file, I both your files should compile... note in class A you have an auto member to class B object, in this case a forward declare would not be sufficient and you would need to include the header file of class B in the header file of class A. most of you include should be done in your source file(.cpp) not your header files, unless you like long builds....man there is just too much to talk about this topic and I don't want to get you lost...LOL :-D

      Yours Truly, The One and Only!

      Z 1 Reply Last reply
      0
      • Z zqueezy

        hey guys, I've got a little problem... I have two classes. One is the member of the other. but the other still needs a pointer to the first. // A.h #include "B.h" class A; class A { public: B m_BClass; }; // B.h #include "A.h" class B; class B { public: A* m_pAClass; // <-- error here }; somehow this doesn't work... He gives me: "Error 4 Error C2143: Syntaxerror: missing ';' before '*'" cause he doesn't know the class A. Though I forward declarated it. A friend of mine told me about an "inclusion deadlock". but I cannot figure out how to solve it. i need to to a global class with all necessary includes. any hints or links? thx in advance PS: should I put the forward deklaration before the "#include" ???

        B Offline
        B Offline
        Bartosz Bien
        wrote on last edited by
        #3

        Hello, // A.h #include "B.h" class A { public: B m_BClass; void SayHelloToMyLittleFriend() {} }; // B.h class A; class B { public: A* m_pAClass; // <-- no error here anymore, A is declared (but not defined) void LetsRock(); }; // B.cpp #include "B.h" #include "A.h" void B::LetsRock() { // TODO: do something crazy with m_pAClass, like calling its member function. m_pAClass->SayHelloToMyLittleFriend(); }

        Best regards, BB http://spin.bartoszbien.com

        D 1 Reply Last reply
        0
        • D DevMentor org

          your problem is you got to forward declare class A is the class B header...this is the only way class B will know about class A, but when using a pointer in class B for class A, you do not have to forward declare, because you are already including the header files for class A inside class B!!! try taking about the forward declare in both file, I both your files should compile... note in class A you have an auto member to class B object, in this case a forward declare would not be sufficient and you would need to include the header file of class B in the header file of class A. most of you include should be done in your source file(.cpp) not your header files, unless you like long builds....man there is just too much to talk about this topic and I don't want to get you lost...LOL :-D

          Yours Truly, The One and Only!

          Z Offline
          Z Offline
          zqueezy
          wrote on last edited by
          #4

          yes I tried that, too. but when I want to edit other members of A in B: void B::Edit() { m_pAClass->somemember = somevalue; } I get, of course, somemember is unknown identifier... ???

          1 Reply Last reply
          0
          • B Bartosz Bien

            Hello, // A.h #include "B.h" class A { public: B m_BClass; void SayHelloToMyLittleFriend() {} }; // B.h class A; class B { public: A* m_pAClass; // <-- no error here anymore, A is declared (but not defined) void LetsRock(); }; // B.cpp #include "B.h" #include "A.h" void B::LetsRock() { // TODO: do something crazy with m_pAClass, like calling its member function. m_pAClass->SayHelloToMyLittleFriend(); }

            Best regards, BB http://spin.bartoszbien.com

            D Offline
            D Offline
            DevMentor org
            wrote on last edited by
            #5

            nice work mate!

            Yours Truly, The One and Only!

            Z 1 Reply Last reply
            0
            • D DevMentor org

              nice work mate!

              Yours Truly, The One and Only!

              Z Offline
              Z Offline
              zqueezy
              wrote on last edited by
              #6

              what if I do not have an implementation file (cpp) because I wanna keep the code in the header? does it work, too? Or do I have to ship everything into an implementation file?

              D 1 Reply Last reply
              0
              • Z zqueezy

                what if I do not have an implementation file (cpp) because I wanna keep the code in the header? does it work, too? Or do I have to ship everything into an implementation file?

                D Offline
                D Offline
                DevMentor org
                wrote on last edited by
                #7

                that should not matter if you put everything into a header file, but in most cases you will want to separate declaration with implementation! .... people just want to see the class members and know how to call it, they don't care how it's implemented and having to read tons of line of code figure out all your methods in a class is not the way to code. if you want to declare everything inside the head file you have to make all the method inline, otherwise you're going to get linking errors. i.e

                // headfile: A.h
                //
                class A {
                public:

                void MyFunc() {
                //write code inline
                }

                };

                // don't do this in the headfile
                void A::MyFunc() {
                // write non-inline code
                }

                I am sure you will figure it out :-D

                Yours Truly, The One and Only!

                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