我创建了一个Global.h文件如下,里面包含了一个全局变量g_sUserDegree。
#ifndef GLOBAL_H_ 
#define GLOBAL_H_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#pragma pack (push ,1)
struct USER_DEGREE{
//资料库部分
unsigned IS_FILE_ADD_ACT : 1;
unsigned IS_FILE_ADDBASE : 1;
unsigned IS_FILE_REPEAT  : 1;
};
#pragma pack (pop)extern USER_DEGREE g_sUserDegree;#endif------------------------------------------------
可是我Global初始化时VC总是提示我找不到USER_DEGREE,怎么回事?Global.cpp#include "Global.h"
#include "stdafx.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifUSER_DEGREE g_sUserDegree;-----------------------------------------------
错误消息:
error C2146: syntax error : missing ';' before identifier 'g_sUserDegree'
error C2501: 'USER_DEGREE' : missing storage-class or type specifiers
fatal error C1004: unexpected end of file found

解决方案 »

  1.   

    extern USER_DEGREE g_sUserDegree;
    改成USER_DEGREE g_sUserDegree;试试
      

  2.   

    如果其他地方要这个变量,再extern USER_DEGREE g_sUserDegree;这样声名
      

  3.   

    我想是因为你包含了#include "Global.h",它先编译.h,当遇到extern USER_DEGREE g_sUserDegree;当然找不到g_sUserDegree了,你应该定义在。h文件中才对。
      

  4.   

    //头文件顺序调整一下,不管什么情况
    stdafx.h一定要放在最前面#include "stdafx.h"
    #include "Global.h"
      

  5.   

    .h中定义USER_DEGREE g_sUserDegree;.cpp中声明 extern USER_DEGREE g_sUserDegree;
      

  6.   

    那就把你的定义都放到stdafx.h文件里,方便些。