How to add a member variable of one class to another class
-
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
-
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
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|
-
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|
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
-
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
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
-
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
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
-
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
It works, Thanks a lot !!!!!!!!!!!!!! Jerzy:) :) :)