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. Class ProtoType

Class ProtoType

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 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.
  • D Offline
    D Offline
    dehseth
    wrote on last edited by
    #1

    Hey everybody, I got problem with prototype. I do have 2 classes both inherited from CDialog. And these two have pointer, which points each other. It seems smt like this: CChild : CDialog { CMain* parent; void doSmt(); }; CMain : CDialog { CChild* child; void doSmt(); }; I got a header file which includes Child first and Main after. OK now my problem is if I do not write class prototype of Main in the header file before including Child header file it gives me error: CMain undeclared identifier which makes sense. But, when I do write prototype: class CMain; #include "CChild.h" #include "CMain.h" then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class. And when I do (try to) write a prototype of DoSmt function: class CMain; void CMain::doSmt(); #include "CChild.h" #include "CMain.h" gives me the same error at line where I define function prototype. So I guess the question is, how can I define prototype of my class function :confused: Thanks.... :)

    C R 2 Replies Last reply
    0
    • D dehseth

      Hey everybody, I got problem with prototype. I do have 2 classes both inherited from CDialog. And these two have pointer, which points each other. It seems smt like this: CChild : CDialog { CMain* parent; void doSmt(); }; CMain : CDialog { CChild* child; void doSmt(); }; I got a header file which includes Child first and Main after. OK now my problem is if I do not write class prototype of Main in the header file before including Child header file it gives me error: CMain undeclared identifier which makes sense. But, when I do write prototype: class CMain; #include "CChild.h" #include "CMain.h" then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class. And when I do (try to) write a prototype of DoSmt function: class CMain; void CMain::doSmt(); #include "CChild.h" #include "CMain.h" gives me the same error at line where I define function prototype. So I guess the question is, how can I define prototype of my class function :confused: Thanks.... :)

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      dehseth wrote:

      class CMain; #include "CChild.h" #include "CMain.h" then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class.

      What is the source file compiling (whenever this error occurs)?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      D 1 Reply Last reply
      0
      • D dehseth

        Hey everybody, I got problem with prototype. I do have 2 classes both inherited from CDialog. And these two have pointer, which points each other. It seems smt like this: CChild : CDialog { CMain* parent; void doSmt(); }; CMain : CDialog { CChild* child; void doSmt(); }; I got a header file which includes Child first and Main after. OK now my problem is if I do not write class prototype of Main in the header file before including Child header file it gives me error: CMain undeclared identifier which makes sense. But, when I do write prototype: class CMain; #include "CChild.h" #include "CMain.h" then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class. And when I do (try to) write a prototype of DoSmt function: class CMain; void CMain::doSmt(); #include "CChild.h" #include "CMain.h" gives me the same error at line where I define function prototype. So I guess the question is, how can I define prototype of my class function :confused: Thanks.... :)

        R Offline
        R Offline
        Rajkumar R
        wrote on last edited by
        #3

        you need the forward declaration in the declaration file, say in CMain.h, declare CMain.h: class CChild; // forward declaration. CMain : CDialog { CChild* child; void doSmt(); }; remove the forward declaration in the cpp(I Suppose) CMain.cpp:

        dehseth wrote:

        class CMain; #include "CMain.h" #include "CChild.h" void CMain::doSmt() { } #include "CChild.h" #include "CMain.h"

        D 1 Reply Last reply
        0
        • R Rajkumar R

          you need the forward declaration in the declaration file, say in CMain.h, declare CMain.h: class CChild; // forward declaration. CMain : CDialog { CChild* child; void doSmt(); }; remove the forward declaration in the cpp(I Suppose) CMain.cpp:

          dehseth wrote:

          class CMain; #include "CMain.h" #include "CChild.h" void CMain::doSmt() { } #include "CChild.h" #include "CMain.h"

          D Offline
          D Offline
          dehseth
          wrote on last edited by
          #4

          didn't worked.....still says undeclared identifier.. :((

          R 1 Reply Last reply
          0
          • C CPallini

            dehseth wrote:

            class CMain; #include "CChild.h" #include "CMain.h" then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class.

            What is the source file compiling (whenever this error occurs)?

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            D Offline
            D Offline
            dehseth
            wrote on last edited by
            #5

            source code is a lil bit long it makes more confusion if I paste it in here. this error occurs during compiling: I have 2 classes which points each other with pointers. During compile I include child.h first and main.h later. Since child class includes a main class pointer in itself it asks for the class. So I need to write a prototype for mainclass before including child class. When I write class CMain; it asks for Main class functions, cause child class calls function of main class. And here my problem starts cause compiler says undeclare identifier of CMain class... So how can I declare a working prototype of CMain class to compile whole code :confused:

            C 1 Reply Last reply
            0
            • D dehseth

              didn't worked.....still says undeclared identifier.. :((

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              yes that can happen for the other CChild.cpp, if you understand what forward declaration can do, you can alone fix the problem. if you want from me, say what are the files cpp, h and how you declare

              1 Reply Last reply
              0
              • D dehseth

                source code is a lil bit long it makes more confusion if I paste it in here. this error occurs during compiling: I have 2 classes which points each other with pointers. During compile I include child.h first and main.h later. Since child class includes a main class pointer in itself it asks for the class. So I need to write a prototype for mainclass before including child class. When I write class CMain; it asks for Main class functions, cause child class calls function of main class. And here my problem starts cause compiler says undeclare identifier of CMain class... So how can I declare a working prototype of CMain class to compile whole code :confused:

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Deferred declaration (the one you used for class CMain should work, but you've define methods containing references to CMain methods inside CChild source (not inside the header). for instance:

                //FILE: B.h
                class A;
                class B
                {
                A * _pA;

                B(pA){_pA=pA;}
                void DoSomethingWithA();
                };
                //////////////////////////////////
                //FILE B.cpp
                void B::DoSomethingWithA()
                {
                _pA->DoSomething();
                }

                should work, while //FILE: B.h class A; class B { A * _pA; B(pA){_pA=pA;} void DoSomethingWithA() { _pA->DoSomething(); // ERROR, UNDEFINED REFERENCE } }; shouldn't. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                D 1 Reply Last reply
                0
                • C CPallini

                  Deferred declaration (the one you used for class CMain should work, but you've define methods containing references to CMain methods inside CChild source (not inside the header). for instance:

                  //FILE: B.h
                  class A;
                  class B
                  {
                  A * _pA;

                  B(pA){_pA=pA;}
                  void DoSomethingWithA();
                  };
                  //////////////////////////////////
                  //FILE B.cpp
                  void B::DoSomethingWithA()
                  {
                  _pA->DoSomething();
                  }

                  should work, while //FILE: B.h class A; class B { A * _pA; B(pA){_pA=pA;} void DoSomethingWithA() { _pA->DoSomething(); // ERROR, UNDEFINED REFERENCE } }; shouldn't. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  D Offline
                  D Offline
                  dehseth
                  wrote on last edited by
                  #8

                  ok! I got a lil bit confused right now... You sound right. I need to include header files.. Well I did writye the source code bottom of header files which is wrong, I get that now! Now my problem continues... These are the files I have got: Main.cpp Child.cpp MSFLexGrid.cpp Application.cpp I include header files in each of them correctly. Compling each one one by one does not create any errors. But when I do Build, if says error LNK2005: "int __cdecl getSize(char *)" (?getSize@@YAHPAD@Z) already defined in App.obj cause it's already included in cpp file... So Do I have to write #ifndef directives to each header file :confused: How can I build without having any linker errors?? Thanks... :)

                  C 1 Reply Last reply
                  0
                  • D dehseth

                    ok! I got a lil bit confused right now... You sound right. I need to include header files.. Well I did writye the source code bottom of header files which is wrong, I get that now! Now my problem continues... These are the files I have got: Main.cpp Child.cpp MSFLexGrid.cpp Application.cpp I include header files in each of them correctly. Compling each one one by one does not create any errors. But when I do Build, if says error LNK2005: "int __cdecl getSize(char *)" (?getSize@@YAHPAD@Z) already defined in App.obj cause it's already included in cpp file... So Do I have to write #ifndef directives to each header file :confused: How can I build without having any linker errors?? Thanks... :)

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    How did you define such function? Where?

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                    D 1 Reply Last reply
                    0
                    • C CPallini

                      How did you define such function? Where?

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                      D Offline
                      D Offline
                      dehseth
                      wrote on last edited by
                      #10

                      ok I have solved the problem! I've got linker error problems because I am defining Message Map declerations in header file. I just move BEGIN_MESSAGE_MAP parts into cpp file and my problem is solved! :laugh: Thank you for your help! :)

                      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