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. Working with two classes

Working with two classes

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.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, I have a class and one of it has a function as follows.

    void CMsgRecorder::SetGroupState(char chState) // Eranga
    {
    // do the processing
    }

    defining as follows, class CMsgRecorder { public: void SetGroupState(char chState); } Other class has following function.

    void CRfSvrDriver::ShowInfo()
    {
    //call SetGroupState() from here
    }

    It's also define as public function in CRfSvrDriver class. How can I call SetGroupState() from the ShowInfo() which are in two classes. Help really appreciate :)

    I appreciate your help all the time... Eranga :)

    N 1 Reply Last reply
    0
    • C CodingLover

      Hi all, I have a class and one of it has a function as follows.

      void CMsgRecorder::SetGroupState(char chState) // Eranga
      {
      // do the processing
      }

      defining as follows, class CMsgRecorder { public: void SetGroupState(char chState); } Other class has following function.

      void CRfSvrDriver::ShowInfo()
      {
      //call SetGroupState() from here
      }

      It's also define as public function in CRfSvrDriver class. How can I call SetGroupState() from the ShowInfo() which are in two classes. Help really appreciate :)

      I appreciate your help all the time... Eranga :)

      N Offline
      N Offline
      Nitheesh George
      wrote on last edited by
      #2

      use inheritance. class CMsgRecorder { public: void SetGroupState(char chState) { do-something; } }; class CRfSvrDriver : public CMsgRecorder { public: void ShowInfo() { SetGroupState();//calls the base function CMsgRecorder::SetGroupState } };

      C 1 Reply Last reply
      0
      • N Nitheesh George

        use inheritance. class CMsgRecorder { public: void SetGroupState(char chState) { do-something; } }; class CRfSvrDriver : public CMsgRecorder { public: void ShowInfo() { SetGroupState();//calls the base function CMsgRecorder::SetGroupState } };

        C Offline
        C Offline
        CodingLover
        wrote on last edited by
        #3

        Yes I do, but the question is this. I already use one class there as follows.

        class CRfSvrDriver :
        public CCmdDriver

        How can I used another class, how to separate those two

        I appreciate your help all the time... Eranga :)

        N C S 3 Replies Last reply
        0
        • C CodingLover

          Yes I do, but the question is this. I already use one class there as follows.

          class CRfSvrDriver :
          public CCmdDriver

          How can I used another class, how to separate those two

          I appreciate your help all the time... Eranga :)

          N Offline
          N Offline
          Nitheesh George
          wrote on last edited by
          #4

          then why don't you try this class CRfSvrDriver :public CCmdDriver,public MsgRecorder OR u want to maintain two separate version of the class CRfSvrDriver ?

          1 Reply Last reply
          0
          • C CodingLover

            Yes I do, but the question is this. I already use one class there as follows.

            class CRfSvrDriver :
            public CCmdDriver

            How can I used another class, how to separate those two

            I appreciate your help all the time... Eranga :)

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #5

            Ok, I can separate by a comma and do it. But then I comes with a constructor issue. I can't change my constructor now. So I change my plan, simply used a pointer to the other class as follows. Include the CMsgRecorder header file in the ShowInfo header file.

            void CRfSvrDriver::ShowInfo(CMsgRecorder* msgRec)
            {
            msgRec->SetGroupState('R');
            }

            But it gives a compile error.

            error LNK2019: unresolved external symbol "public: void __thiscall CMsgRecorder::SetGroupState(char)"
            (?SetGroupState@CMsgRecorder@@QAEXD@Z) referenced in function "public: int __thiscall
            CRfSvrDriver::ProcessCommand(class ATL::CStringT >

            ,unsigned int,class CMsgRecorder *)"
            (?ProcessCommand@CRfSvrDriver@@QAEHV?$CStringT@DV?$StrTraitMFC_DLL@DV?$
            ChTraitsCRT@D@ATL@@@@@ATL@@IPAVCMsgRecorder@@@Z)

            I hope it is linker error, but all functions are declared correctly. You have any suggestion for me on this.

            I appreciate your help all the time... Eranga :)

            N 1 Reply Last reply
            0
            • C CodingLover

              Yes I do, but the question is this. I already use one class there as follows.

              class CRfSvrDriver :
              public CCmdDriver

              How can I used another class, how to separate those two

              I appreciate your help all the time... Eranga :)

              S Offline
              S Offline
              sheshidar
              wrote on last edited by
              #6

              class CMsgRecorder { public: void SetGroupState(char chState) { do-something; } }; Now you can use multi-one inheritance like this class CRfSvrDriver : public CMsgRecorder,public CCmdDriver { public: void ShowInfo() { SetGroupState();//calls the base function CMsgRecorder::SetGroupState } };

              1 Reply Last reply
              0
              • C CodingLover

                Ok, I can separate by a comma and do it. But then I comes with a constructor issue. I can't change my constructor now. So I change my plan, simply used a pointer to the other class as follows. Include the CMsgRecorder header file in the ShowInfo header file.

                void CRfSvrDriver::ShowInfo(CMsgRecorder* msgRec)
                {
                msgRec->SetGroupState('R');
                }

                But it gives a compile error.

                error LNK2019: unresolved external symbol "public: void __thiscall CMsgRecorder::SetGroupState(char)"
                (?SetGroupState@CMsgRecorder@@QAEXD@Z) referenced in function "public: int __thiscall
                CRfSvrDriver::ProcessCommand(class ATL::CStringT >

                ,unsigned int,class CMsgRecorder *)"
                (?ProcessCommand@CRfSvrDriver@@QAEHV?$CStringT@DV?$StrTraitMFC_DLL@DV?$
                ChTraitsCRT@D@ATL@@@@@ATL@@IPAVCMsgRecorder@@@Z)

                I hope it is linker error, but all functions are declared correctly. You have any suggestion for me on this.

                I appreciate your help all the time... Eranga :)

                N Offline
                N Offline
                Nitheesh George
                wrote on last edited by
                #7

                the linker cannot find the definition of SetGroupState that is the problem. whether u declared SetGroupState as a static function? or not defined it. And another thing i don't think the method u opted is a good programming practice. Inheritance is the good method in such situations.

                C 1 Reply Last reply
                0
                • N Nitheesh George

                  the linker cannot find the definition of SetGroupState that is the problem. whether u declared SetGroupState as a static function? or not defined it. And another thing i don't think the method u opted is a good programming practice. Inheritance is the good method in such situations.

                  C Offline
                  C Offline
                  CodingLover
                  wrote on last edited by
                  #8

                  Ok pal. Thanks. Actually I missed one thing there. Those two classes are in two projects. I think that is why there is a linker error. What I can do now. :(

                  I appreciate your help all the time... Eranga :)

                  N 1 Reply Last reply
                  0
                  • C CodingLover

                    Ok pal. Thanks. Actually I missed one thing there. Those two classes are in two projects. I think that is why there is a linker error. What I can do now. :(

                    I appreciate your help all the time... Eranga :)

                    N Offline
                    N Offline
                    Nitheesh George
                    wrote on last edited by
                    #9

                    Then make a DLL of the project that contains class CMsgRecorder. Then include .h file of the class and lib file in your new project. Then compile it. ie's it.

                    C 1 Reply Last reply
                    0
                    • N Nitheesh George

                      Then make a DLL of the project that contains class CMsgRecorder. Then include .h file of the class and lib file in your new project. Then compile it. ie's it.

                      C Offline
                      C Offline
                      CodingLover
                      wrote on last edited by
                      #10

                      I'll see, because working with DLLs are really new work for me. :(

                      I appreciate your help all the time... Eranga :)

                      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