0

sorry that my english skill is baddest.

when i use c# , i dont't crazy global variable ( no problems used otehr projects static variable in member but when use static variable in MFC ,

i have a problem neither global variabel or static member variable problem too , it's LNK2019 and LNK2001 unreserved external symbols error .

when i use static variable i have declared of members

  • .h Source *

    #pragma once
    #include "../CUstomCtl/CUstomCtlDlg.h"
    
    extern CCUstomCtlDlg CusDlg; // this declaration is  when i use for 
                                 // global variable
    class CGlobalClass
    {
     public:
     CGlobalClass(void);
    
    ~CGlobalClass(void);
    
     //static CCUstomCtlDlg CusDlg;  // i want CusDlg use to other project 
                                     // like static varialble
    };
    
  • .cpp Source *

       #pragma once
       #include "StdAfx.h"
       #include "GlobalClass.h"
       #include "../CUstomCtl/CUstomCtlDlg.h" // CCUstomCtlDlg included
    
    
      CCUstomCtlDlg CusDlg = NULL; // declared like this
    
      CGlobalClass::CGlobalClass(void)
      {
      }
    
    
      CGlobalClass::~CGlobalClass(void)
      {
      }
    

    // i use in other project global variable like this

       #include "../MyGlobal/GlobalClass.h" // i had a include
    
      void CtestDlg::OnBnClickedButton1()
      {
         // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
    
         CusDlg.DoModal(); // LNK error !! crazyed me .
      }
    
  • i want know Two case in MFC *

  1. using in global variable to other project not own project
  2. using in static member variable to other project not own project

please help me my head will be explosion soon .

Theremin
  • 15
  • 4
  • Before you ask a question like this, you first priority should be to figure out what language you are in, because you seem confused. "*java c# c++ c mfc*" – TheGeneral Feb 10 '21 at 07:19

1 Answers1

0

This is how to do a global variable

.h source

extern CCUstomCtlDlg CusDlg;

.cpp source

CCUstomCtlDlg CusDlg;

This is how to do a static class variable

.h source

class CGlobalClass
{
    static CCUstomCtlDlg CusDlg;
};

.cpp source

CCUstomCtlDlg CGlobalClass::CusDlg;
john
  • 85,011
  • 4
  • 57
  • 81
  • thanks your answers , when use other project this global or static variable occurred LNK error , so i crazy ^^ – Theremin Feb 10 '21 at 07:36
  • @Theremin Then you made a mistake somewhere else. Because the code above is how you do it. Why don't you try first on a very small project, until you are confident you know how to do this. Then you can try the same technique on a larger project. – john Feb 10 '21 at 07:39
  • @Theremin Your code for the global variable is wrong. Can you see the difference between your code and my code? – john Feb 10 '21 at 07:40
  • i have a lot of career with MFC but using MFC is long time no see , sorry my skill is bad.^^ – Theremin Feb 10 '21 at 07:42
  • yes . i want show your code , i will have reserved my codes to learn *^^* – Theremin Feb 10 '21 at 07:46
  • i have no problem using own project but when i use other project occurred this problems . why ?? – Theremin Feb 10 '21 at 07:56
  • @Theremin That's impossible for me to say, – john Feb 10 '21 at 07:58
  • sorry i had don't know this is your answer code . my questions' source is repaired. but i wonder why own project is no problem but when i use the another project occurred LNK ERROR , i set path include header file. – Theremin Feb 10 '21 at 08:12
  • @Theremin Please post the **exact** error message. Include paths are nothing to do with linker errors. – john Feb 10 '21 at 08:18
  • i make a new project named MyDlg error is 1>GlobalClass.obj : error LNK2019: "public: __thiscall CMyDlgDlg::CMyDlgDlg(class CWnd *)" (??0CMyDlgDlg@@QAE@PAVCWnd@@@Z) 외부 기호(참조 위치: "void __cdecl `dynamic initializer for 'public: static class CMyDlgDlg CGlobalClass::myDlg''(void)" (??__E?myDlg@CGlobalClass@@2VCMyDlgDlg@@A@@YAXXZ) 함수)에서 확인하지 못했습니다. 1>C:\Users\ABC\Documents\Visual Studio 2010\Projects\test\Debug\MyGlobal.exe : fatal error LNK1120: 1개의 확인할 수 없는 외부 참조입니다. 1> – Theremin Feb 10 '21 at 09:30
  • why error occurred in CMyDlgDlg Constructor ?? – Theremin Feb 10 '21 at 09:32
  • @Theremin Because that error message is about a missing constructor definition, not about a static or global variable. – john Feb 10 '21 at 09:34
  • @Theremin The missing deifnition is for `CMyDlgDlg::CMyDlgDlg(CWnd *)`. – john Feb 10 '21 at 09:35
  • Constructor is auto implemented . CMyDlgDlg::CMyDlgDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CMyDlgDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } – Theremin Feb 10 '21 at 09:42
  • #include "../MyGlobal/GlobalClass.h" // .declared header and i am call void CtestDlg::OnBnClickedButton1() { // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다. CGlobalClass::myDlg->DoModal(); } – Theremin Feb 10 '21 at 09:46
  • now error is Two one is : 모든 출력이 최신 상태입니다. 2>testDlg.obj : error LNK2001: "public: static class CMyDlgDlg * CGlobalClass::myDlg" (?myDlg@CGlobalClass@@2PAVCMyDlgDlg@@A) 외부 기호를 확인할 수 없습니다. 2>C:\Users\ABC\Documents\Visual Studio 2010\Projects\test\Debug\test.exe : fatal error LNK1120: 1개의 확인할 수 없는 외부 참조입니다. – Theremin Feb 10 '21 at 09:48
  • two is : GlobalClass.obj : error LNK2019: "public: __thiscall CMyDlgDlg::CMyDlgDlg(class CWnd *)" (??0CMyDlgDlg@@QAE@PAVCWnd@@@Z) 외부 기호(참조 위치: "void __cdecl `dynamic initializer for 'public: static class CMyDlgDlg * CGlobalClass::myDlg''(void)" (??__E?myDlg@CGlobalClass@@2PAVCMyDlgDlg@@A@@YAXXZ) 함수)에서 확인하지 못했습니다. 1>C:\Users\ABC\Documents\Visual Studio 2010\Projects\test\Debug\MyGlobal.exe : fatal error LNK1120: 1개의 확인할 수 없는 외부 참조입니다. – Theremin Feb 10 '21 at 09:48
  • Thank you john very helpful. Thank you. I will be stutying this error. Good luck friend.~ – Theremin Feb 10 '21 at 10:02