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. How to add a member variable of one class to another class

How to add a member variable of one class to another class

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++tutorialquestion
6 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.
  • J Offline
    J Offline
    JerzyPeter
    wrote on last edited by
    #1

    I have SDI app with Doc/View support. I want to add a member variable m_MyList (based on a CListCtrl's derived class CMyListCtrl) to CMyView class. So i put #include "MyListCtrl.h" in MyView.h (otherwise compler won't know what m_MyList is) ********************************************* CMyView.h: #include "MyListCtrl.h" class CMyView : public CView { ... CMyDoc* GetDocument(); //error C2501 ... CMyListCtrl m_MyList; ... }; ***************************************** In MyListCtrl.cpp I want to get the pointer to CMyView like this: CMyView *v=(CMyView *)GetParent() But compiler will complain that it doesn't know what CMyView is. The moment I #include "MyView.h" on the top of MyListCtrl.cpp I'm getting error C2143 and C2501. (Because it it circular refrence). How to solve this problem? By the way, (assuming I don't have my new class CMyListCtrl yet) why line CMyDoc* GetDocument(); dosn't bomb ???????? How does compiler know what CMyDoc is ?????. There's no #include "MyDoc.h" in CMyView class definition. error C2143: syntax error : missing ';' before '*' error C2501: 'CMyDoc' : missing storage-class or type specifiers Any comments? Thanks, Jerzy

    N 1 Reply Last reply
    0
    • J JerzyPeter

      I have SDI app with Doc/View support. I want to add a member variable m_MyList (based on a CListCtrl's derived class CMyListCtrl) to CMyView class. So i put #include "MyListCtrl.h" in MyView.h (otherwise compler won't know what m_MyList is) ********************************************* CMyView.h: #include "MyListCtrl.h" class CMyView : public CView { ... CMyDoc* GetDocument(); //error C2501 ... CMyListCtrl m_MyList; ... }; ***************************************** In MyListCtrl.cpp I want to get the pointer to CMyView like this: CMyView *v=(CMyView *)GetParent() But compiler will complain that it doesn't know what CMyView is. The moment I #include "MyView.h" on the top of MyListCtrl.cpp I'm getting error C2143 and C2501. (Because it it circular refrence). How to solve this problem? By the way, (assuming I don't have my new class CMyListCtrl yet) why line CMyDoc* GetDocument(); dosn't bomb ???????? How does compiler know what CMyDoc is ?????. There's no #include "MyDoc.h" in CMyView class definition. error C2143: syntax error : missing ';' before '*' error C2501: 'CMyDoc' : missing storage-class or type specifiers Any comments? Thanks, Jerzy

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      Instead of including "MyListCtrl.h" in MyView.h, put the next line: class CMyListCtrl; // just to let compiler know about your type and include "MyListCtrl.h" from the cpp file. I vote pro drink X|

      J 1 Reply Last reply
      0
      • N Nemanja Trifunovic

        Instead of including "MyListCtrl.h" in MyView.h, put the next line: class CMyListCtrl; // just to let compiler know about your type and include "MyListCtrl.h" from the cpp file. I vote pro drink X|

        J Offline
        J Offline
        JerzyPeter
        wrote on last edited by
        #3

        I’m afraid it still doesn’t work. CMyView.h file: class CMyListCtrl; class CMyView : public CView { ... CMyListCtrl m_MyList; ...}; I´m getting error C2079: ´m_MyList´ uses undefined class ´CMyListCtrl´ Any ideas? Thanks, Jerzy

        L 2 Replies Last reply
        0
        • J JerzyPeter

          I’m afraid it still doesn’t work. CMyView.h file: class CMyListCtrl; class CMyView : public CView { ... CMyListCtrl m_MyList; ...}; I´m getting error C2079: ´m_MyList´ uses undefined class ´CMyListCtrl´ Any ideas? Thanks, Jerzy

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The best way is to make the member variable a pointer to the class class CMyListCtrl; // instead of #include "MyListCtrl.h" class CMyView : public CView { .. .. CMyListCtrl* m_pMyList; . . . } In the constructor of the CMyView class, you can use m_pMyList = new CMyListCtrl; - Thomas

          1 Reply Last reply
          0
          • J JerzyPeter

            I’m afraid it still doesn’t work. CMyView.h file: class CMyListCtrl; class CMyView : public CView { ... CMyListCtrl m_MyList; ...}; I´m getting error C2079: ´m_MyList´ uses undefined class ´CMyListCtrl´ Any ideas? Thanks, Jerzy

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            The best way is to make the member variable a pointer to the class class CMyListCtrl; // instead of #include "MyListCtrl.h" class CMyView : public CView { .. .. CMyListCtrl* m_pMyList; . . . } In the constructor of the CMyView class, you can use m_pMyList = new CMyListCtrl; - Thomas

            J 1 Reply Last reply
            0
            • L Lost User

              The best way is to make the member variable a pointer to the class class CMyListCtrl; // instead of #include "MyListCtrl.h" class CMyView : public CView { .. .. CMyListCtrl* m_pMyList; . . . } In the constructor of the CMyView class, you can use m_pMyList = new CMyListCtrl; - Thomas

              J Offline
              J Offline
              JerzyPeter
              wrote on last edited by
              #6

              It works, Thanks a lot !!!!!!!!!!!!!! Jerzy:) :) :)

              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