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 come this happen? thank you very much

how come this happen? thank you very much

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionlearning
5 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.
  • E Offline
    E Offline
    ewighell
    wrote on last edited by
    #1

    I build a dialog based MFC program. in the file "StdAfx.h" I add the following code,which is used to define a global variable of type CString.

    #ifndef jjjjjjjjjjjjjjjjjjj
    #define jjjjjjjjjjjjjjjjjjj
    CString StrGlobal;
    #endif

    and then I use it in another file "newfile.cpp"

    #include "stdafx.h"
    extern CString StrGlobal;
    void fun()
    {
    StrGlobal = _T("KDKDK");
    }

    when I build the program, following link error shows up: Linking... StdAfx.obj : error LNK2005: "class CString StrGlobal" (?StrGlobal@@3VCString@@A) already defined in newfile.obj why dose this happen? and how can I fix it ? Thank you very much!!! ------------------- I am learning C++ and English

    N N 2 Replies Last reply
    0
    • E ewighell

      I build a dialog based MFC program. in the file "StdAfx.h" I add the following code,which is used to define a global variable of type CString.

      #ifndef jjjjjjjjjjjjjjjjjjj
      #define jjjjjjjjjjjjjjjjjjj
      CString StrGlobal;
      #endif

      and then I use it in another file "newfile.cpp"

      #include "stdafx.h"
      extern CString StrGlobal;
      void fun()
      {
      StrGlobal = _T("KDKDK");
      }

      when I build the program, following link error shows up: Linking... StdAfx.obj : error LNK2005: "class CString StrGlobal" (?StrGlobal@@3VCString@@A) already defined in newfile.obj why dose this happen? and how can I fix it ? Thank you very much!!! ------------------- I am learning C++ and English

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      move the declaration CString StrGlobal; to stdafx.cpp or make the varaiable static nave

      E 1 Reply Last reply
      0
      • N Naveen

        move the declaration CString StrGlobal; to stdafx.cpp or make the varaiable static nave

        E Offline
        E Offline
        ewighell
        wrote on last edited by
        #3

        yes, I make it static and it works. thank you so much. but ,could you please explain to me briefly the reason? ------------------- I am learning C++ and English

        N 1 Reply Last reply
        0
        • E ewighell

          yes, I make it static and it works. thank you so much. but ,could you please explain to me briefly the reason? ------------------- I am learning C++ and English

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          The .h file might have been include by multiple cpp file. so while compiling , the variable will be present in the vtable of obj file corresponding to each cpp. so while linking, linker will find multiple object and generates the error since it dosen't know to which obj it have to link. nave

          1 Reply Last reply
          0
          • E ewighell

            I build a dialog based MFC program. in the file "StdAfx.h" I add the following code,which is used to define a global variable of type CString.

            #ifndef jjjjjjjjjjjjjjjjjjj
            #define jjjjjjjjjjjjjjjjjjj
            CString StrGlobal;
            #endif

            and then I use it in another file "newfile.cpp"

            #include "stdafx.h"
            extern CString StrGlobal;
            void fun()
            {
            StrGlobal = _T("KDKDK");
            }

            when I build the program, following link error shows up: Linking... StdAfx.obj : error LNK2005: "class CString StrGlobal" (?StrGlobal@@3VCString@@A) already defined in newfile.obj why dose this happen? and how can I fix it ? Thank you very much!!! ------------------- I am learning C++ and English

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            ewighell wrote:

            #ifndef jjjjjjjjjjjjjjjjjjj #define jjjjjjjjjjjjjjjjjjj CString StrGlobal; #endif

            #ifndef jjjjjjjjjjjjjjjjjjj
            #define jjjjjjjjjjjjjjjjjjj
            extern CString StrGlobal;
            #endif

            ewighell wrote:

            #include "stdafx.h" extern CString StrGlobal; void fun() { StrGlobal = _T("KDKDK"); }

            #include "stdafx.h"
            CString StrGlobal;
            void fun()
            {
            StrGlobal = _T("KDKDK");
            }


            Nibu thomas Software Developer

            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