C++ 带有外部变量的全局变量声明
我的问题在于以下方面: 文件1.hC++ 带有外部变量的全局变量声明,c++,visual-studio,linker,extern,unresolved-external,C++,Visual Studio,Linker,Extern,Unresolved External,我的问题在于以下方面: 文件1.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, r
#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中,应用程序崩溃,因为图wnd\u sensor1D的信息与前一个不同。
在文件2.cpp中,我想使用graph_wnd_sensor1D。但视觉收益率
CMyTabCtrl.obj : error LNK2001: external symbol unresolved "class CGraphWnd graph_wnd_sensor1D"
- 因此,我们的想法是让graph_wnd_sensor1D成为一个全局变量,它在文件1中声明!
我怎样才能解决这个问题*李>
extern CGraphWnd graph_wnd_sensor1D; // declarations
文件2.cpp
CGraphWnd graph_wnd_sensor1D; // definition
使用此选项,它当然可以进行编译,但graph_wnd_sensor1D没有在文件1中设置的值。cpp@djfoxmccloud你怎么知道的?你在输出吗?是否有另一个同名的局部变量?您是否以某种方式声明了它?在文件1.h中,我首先声明它为CGraphWnd graph\u wnd\u sensor1D;(请参见OP)然后在file1.cpp中使用它并具有一个值。然后我在file2.cpp中调用我的函数,该函数使用graph_wnd_sensor1D,但它没有与file1.cpp中的函数相同的信息,即使它在任何地方都没有修改。因此,file2.cpp中的定义以某种方式覆盖了variable@djfoxmccloud“file1.h我首先声明它为CGraphWnd graph\u wnd\u sensor1D”-
CGraphWnd graph\u wnd\u sensor1D代码>不是一个声明,而是一个定义。如果我从file1.cpp graph\u wnd\u sensor1D右键单击,则需要使用extern
@LuchianGrigore在视觉上声明它,显示声明和显示定义指向CGraphWnd graph\u wnd\u sensor1D;在file1.h中。我已经更新了OP了解更多信息