including .cpp file
-
I have global variables float xtrans, ytrans; defined in my file TestGLView.cpp and I have this function for handling keyboard events in my file ChildFrm.cpp void CChildFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default CMDIChildWnd::OnKeyDown(nChar, nRepCnt, nFlags); char keypressed = char(nChar); if(keypressed == 'I') ytrans += 0.2; if(keypressed == 'K') ytrans -= 0.2; } this function uses the global variables from TestGLView.cpp so I need to #include "TestGLView.cpp" in the file ChildFrm.cpp but when I do that, I get this error: C:\WINDOWS\Desktop\QE2 heart program\ChildFrm.cpp(14) : error C2370: 'THIS_FILE' : redefinition; different storage class c:\windows\desktop\qe2 heart program\testglview.cpp(22) : see declaration of 'THIS_FILE' so I comment out this line from ChildFrm.cpp since its present at the top of both files: static char THIS_FILE[] = __FILE__; but then I get tons of other errors, here are some: TestGLView.obj : error LNK2005: "public: static class CObject * __stdcall CTestGLView::CreateObject(void)" (?CreateObject@CTestGLView@@SGPAVCObject@@XZ) already defined in ChildFrm.obj TestGLView.obj : error LNK2005: "protected: static struct CRuntimeClass * __stdcall CTestGLView::_GetBaseClass(void)" (?_GetBaseClass@CTestGLView@@KGPAUCRuntimeClass@@XZ) already defined in ChildFrm.obj TestGLView.obj : error LNK2005: "public: virtual struct CRuntimeClass * __thiscall CTestGLView::GetRuntimeClass(void)const " (?GetRuntimeClass@CTestGLView@@UBEPAUCRuntimeClass@@XZ) already defined in ChildFrm.obj So how can I alter the global variables from another file without having to deal with all this? There must be an easier way???
-
I have global variables float xtrans, ytrans; defined in my file TestGLView.cpp and I have this function for handling keyboard events in my file ChildFrm.cpp void CChildFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default CMDIChildWnd::OnKeyDown(nChar, nRepCnt, nFlags); char keypressed = char(nChar); if(keypressed == 'I') ytrans += 0.2; if(keypressed == 'K') ytrans -= 0.2; } this function uses the global variables from TestGLView.cpp so I need to #include "TestGLView.cpp" in the file ChildFrm.cpp but when I do that, I get this error: C:\WINDOWS\Desktop\QE2 heart program\ChildFrm.cpp(14) : error C2370: 'THIS_FILE' : redefinition; different storage class c:\windows\desktop\qe2 heart program\testglview.cpp(22) : see declaration of 'THIS_FILE' so I comment out this line from ChildFrm.cpp since its present at the top of both files: static char THIS_FILE[] = __FILE__; but then I get tons of other errors, here are some: TestGLView.obj : error LNK2005: "public: static class CObject * __stdcall CTestGLView::CreateObject(void)" (?CreateObject@CTestGLView@@SGPAVCObject@@XZ) already defined in ChildFrm.obj TestGLView.obj : error LNK2005: "protected: static struct CRuntimeClass * __stdcall CTestGLView::_GetBaseClass(void)" (?_GetBaseClass@CTestGLView@@KGPAUCRuntimeClass@@XZ) already defined in ChildFrm.obj TestGLView.obj : error LNK2005: "public: virtual struct CRuntimeClass * __thiscall CTestGLView::GetRuntimeClass(void)const " (?GetRuntimeClass@CTestGLView@@UBEPAUCRuntimeClass@@XZ) already defined in ChildFrm.obj So how can I alter the global variables from another file without having to deal with all this? There must be an easier way???
This is what the 'extern' keyword is used for. In your ChildFrm.cpp file, JUST use the following (no need for #include "TestGLView.cpp"): extern float xtrans, ytrans; Cheers, Free
-
I have global variables float xtrans, ytrans; defined in my file TestGLView.cpp and I have this function for handling keyboard events in my file ChildFrm.cpp void CChildFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default CMDIChildWnd::OnKeyDown(nChar, nRepCnt, nFlags); char keypressed = char(nChar); if(keypressed == 'I') ytrans += 0.2; if(keypressed == 'K') ytrans -= 0.2; } this function uses the global variables from TestGLView.cpp so I need to #include "TestGLView.cpp" in the file ChildFrm.cpp but when I do that, I get this error: C:\WINDOWS\Desktop\QE2 heart program\ChildFrm.cpp(14) : error C2370: 'THIS_FILE' : redefinition; different storage class c:\windows\desktop\qe2 heart program\testglview.cpp(22) : see declaration of 'THIS_FILE' so I comment out this line from ChildFrm.cpp since its present at the top of both files: static char THIS_FILE[] = __FILE__; but then I get tons of other errors, here are some: TestGLView.obj : error LNK2005: "public: static class CObject * __stdcall CTestGLView::CreateObject(void)" (?CreateObject@CTestGLView@@SGPAVCObject@@XZ) already defined in ChildFrm.obj TestGLView.obj : error LNK2005: "protected: static struct CRuntimeClass * __stdcall CTestGLView::_GetBaseClass(void)" (?_GetBaseClass@CTestGLView@@KGPAUCRuntimeClass@@XZ) already defined in ChildFrm.obj TestGLView.obj : error LNK2005: "public: virtual struct CRuntimeClass * __thiscall CTestGLView::GetRuntimeClass(void)const " (?GetRuntimeClass@CTestGLView@@UBEPAUCRuntimeClass@@XZ) already defined in ChildFrm.obj So how can I alter the global variables from another file without having to deal with all this? There must be an easier way???
Instead of including
TestGLView.cpp
, addextern float xtrans, ytrans;
to
ChildFrm.cpp
, so the compiler knows, that they're implemented in another file of this project. Regards Thomas Finally with Sonork id: 100.10453 Thömmi
Disclaimer:
Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.