带有extern的全局变量声明

时间:2012-05-14 14:33:32

标签: c++ visual-studio linker extern unresolved-external

我的问题出现在以下背景中:

file1.h

#include "graphwnd.h"
#include "file2.h"

class XXX: 
{
....various things....
protected:
CGraphWnd graph_wnd_sensor1D;
}  

file1.cpp

#include "file1.h"
(... various stuff ...)

void main(){
OnInitGraph(&graph_wnd_1_sensor2D, rect_1_sensor2D);
graph_wnd_sensor1D.ShowWindow(SW_HIDE);
myYYY.Init();
}
(... various stuff ...)

此处graph_wnd_sensor1D有一个值,ShowWindow正常工作

文件2.h

extern CGraphWnd graph_wnd_sensor1D;
class YYY: 
{
void YYY::Init(){
graph_wnd_sensor1D.ShowWindow(SW_SHOW);
}
....various things....
}

这里,在init中,应用程序崩溃,因为graph_wnd_sensor1D与前一个信息的信息不同。

在文件2.cpp中我想使用graph_wnd_sensor1D。但是视觉效果

CMyTabCtrl.obj : error LNK2001: external symbol unresolved "class CGraphWnd graph_wnd_sensor1D"
  • 所以我的想法是让graph_wnd_sensor1D成为一个在file1中声明的全局变量! 我该怎么解决这个问题? *

1 个答案:

答案 0 :(得分:6)

您只声明了变量,但未定义变量。在单个实现文件中添加定义。

档案2.h

extern CGraphWnd graph_wnd_sensor1D; // declarations

档案2.cpp

CGraphWnd graph_wnd_sensor1D; // definition
相关问题