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. including .cpp file

including .cpp file

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
3 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.
  • R Offline
    R Offline
    Rajveer
    wrote on last edited by
    #1

    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???

    P T 2 Replies Last reply
    0
    • R Rajveer

      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???

      P Offline
      P Offline
      Phil Speller
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R Rajveer

        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???

        T Offline
        T Offline
        Thomas Freudenberg
        wrote on last edited by
        #3

        Instead of including TestGLView.cpp, add

        extern 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.

        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