加上
#ifndef _MY_H_
#define _MY_H_

解决方案 »

  1.   

    其实,加到stdafx.h中是个不错的选择
      

  2.   

    还是不行呀!链接错误如下
    Linking...
    MainFrm.obj : error LNK2005: "int  number1" (?number1@@3HA) already defined in StdAfx.obj
    MainFrm.obj : error LNK2005: "class CString  str1" (?str1@@3VCString@@A) already defined in StdAfx.obj
    my001View.obj : error LNK2005: "int  number1" (?number1@@3HA) already defined in StdAfx.obj
    my001View.obj : error LNK2005: "class CString  str1" (?str1@@3VCString@@A) already defined in StdAfx.obj
    my.h:
    #ifndef _MY_H_
    #define _MY_H_int number1;
    CString str1;#endif
      

  3.   

    我觉得可以在app或者cmainfrm里面增加这个变量
    afxgetapp()-〉。。就行了
      

  4.   

    我现在的意思是想把一些共用变量,比如:number1,str1定义在一个文件my.h中,那个类要用,直接#include "my.h"就行,但现在的问题是编译出错!不知道问题出在那儿?
      

  5.   

    我原来也碰到过这个问题,可以避免就是在PROJECT->SETTING->LINK->GATEGORY
    选CUSTOMIZE ,然后再选FORCE FILE OUTPUT 

    regarding global variables, as others pointed out that they are not good, you should use them as less as possible. if you do think you have to use global variables, you can do like this: // put all global variables into a '.cpp' file, for example: 'my_global_variables.cpp' 
    int my_global_int1; 
    int my_global_int2; 
    float my_global_float1; 
    ... 
    // then write a header file ('my_global_variables.h') like this: 
    extern int my_global_int1; 
    extern int my_global_int2; 
    extern float my_global_float1; 
    ... you include 'my_global_variables.h' in every file that needs the global variables. when you compile the code, remember to compile and link the 'my_global_variable.cpp' with other files
      

  6.   

    这是因为包含文件重复使用头文件定义的结果,你可以用以下形式解决重复定义的问题:
    #ifndef mydata
    #define mydata
    ...
    数据定义
    ...
    #endif
      

  7.   

    在头文件中包含变量声明,如:
    extern int mydata;
    在一个源文件(.cpp)中进行定义:
    int mydata = 0;
    为什么要使用全局变量呢?如果只是为了 Frame和View的通信可以定义成View的public变量呀!
      

  8.   

    直接将变量的定义加到StdAfx。h文件中,不会出问题,更不会出现
    MainFrm.obj : error LNK2005: "int  number1" (?number1@@3HA) already defined in StdAfx.obj
    如此错误,我试过的!
      

  9.   

    andy_lau(阿虎):你说的方法我试过,也是不行,你可以给我发一个例子,
    D:\temp\my001\my.cpp(13) : fatal error C1010: unexpected end of file while looking for precompiled header directive
    Error executing cl.exe.
    my001.exe - 1 error(s), 0 warning(s)
    zdl(zx) :也不行。
    那位大虾给我出个主意,我现在定义了好多extern,真的不爽。
      

  10.   

    my.h:中
    #ifndef _MY_H_
    #define _MY_H_
    extern int mydata;
    extern CString str1;
    #endifmy.cpp中
    int number1;
    CString str1;
      

  11.   

    lyoubing(飘在风中的鱼):
    用上面方法会出现如下错误
    D:\temp\my001\my.cpp(3) : fatal error C1010: unexpected end of file while looking for precompiled header directive
      

  12.   

    阿虎:我的问题如下,看到您的解释,我试了一下
    http://www.csdn.net/expert/topic/628/628673.xml?temp=.7219202编译时发现出现下面错误:
    D:\temp\my001\my.cpp(3) : fatal error C1010: unexpected end of file while looking for precompiled header directive
    是什么原因,你解释一下好吗?要不留下你的信箱,我把这个例子给你发过去,你帮忙调试一下,发给我你的邮箱
    还有:remember to compile and link the 'my_global_variable.cpp' with other files  是什么意思,怎么做,我不懂