我想申明一个全局变量,该在哪里申明?
我看到过几种: 
1、在头文件中。我试过,产生重复定义的错误
2、在CMYApp类中,好像也有问题
请指点一下,到底该如何做?

解决方案 »

  1.   

    在头文件中声明,可以用#ifndef #define #endif防止重复定义的错误
    全局变量可以用类成员变量代替的,最好不要使用全局变量
      

  2.   

    CMYApp类中没有问题
    通过((CMYApp*)AfxGetApp())->MyVar访问
      

  3.   

    lphlord(lphlord):能不能说得具体点,
    #ifndef
    #define int MyVar
    #endif是这样吗?
      

  4.   

    不要使用全局变量,用静态变量好了
    //MyApp.h
    Class CMyApp: public CWinApp
    {public:
          static int iCount;
    }//MyApp.cpp--Initialize
    int CWinApp::iCount = 0//Reference
    CWinApp::iCount++;