Working with two classes
-
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 :)
-
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 :)
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 } };
-
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 } };
Yes I do, but the question is this. I already use one class there as follows.
class CRfSvrDriver :
public CCmdDriverHow can I used another class, how to separate those two
I appreciate your help all the time... Eranga :)
-
Yes I do, but the question is this. I already use one class there as follows.
class CRfSvrDriver :
public CCmdDriverHow can I used another class, how to separate those two
I appreciate your help all the time... Eranga :)
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 ?
-
Yes I do, but the question is this. I already use one class there as follows.
class CRfSvrDriver :
public CCmdDriverHow can I used another class, how to separate those two
I appreciate your help all the time... Eranga :)
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 :)
-
Yes I do, but the question is this. I already use one class there as follows.
class CRfSvrDriver :
public CCmdDriverHow can I used another class, how to separate those two
I appreciate your help all the time... Eranga :)
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 } };
-
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 :)
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.
-
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.
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 :)
-
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 :)
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.
-
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.
I'll see, because working with DLLs are really new work for me. :(
I appreciate your help all the time... Eranga :)