3

My problem is in the following context :

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 ...)

here graph_wnd_sensor1D has a value and the ShowWindow works

file 2.h

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

Here, in init, the app crashes because the graph_wnd_sensor1D has not the same information as the previous one.

In file 2.cpp I want to use graph_wnd_sensor1D. But visual yields

CMyTabCtrl.obj : error LNK2001: external symbol unresolved "class CGraphWnd graph_wnd_sensor1D"
  • So the idea is to get graph_wnd_sensor1D to be a global variable which is declared in file1 ! How could I solve this ? *
djfoxmccloud
  • 571
  • 1
  • 9
  • 23

1 Answers1

6

You only declared, but not defined the variable. Add a definition in a single implementation file.

file 2.h

extern CGraphWnd graph_wnd_sensor1D; // declarations

file 2.cpp

CGraphWnd graph_wnd_sensor1D; // definition
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Using this it certainly compiles but the graph_wnd_sensor1D doesn't have the value that is set in file1.cpp – djfoxmccloud May 14 '12 at 14:49
  • @djfoxmccloud how do you know? Are you outputting? Do you have another local variable with the same name? Are you re-declaring it somehow? – Luchian Grigore May 14 '12 at 14:51
  • In file1.h I first declare it as CGraphWnd graph_wnd_sensor1D; (see OP) then it is used in file1.cpp and has a value. Then I call my function in file2.cpp that uses graph_wnd_sensor1D, but it doesn't have the same information as the one in file 1.cpp even It wasn't modified anywhere. So the definition in file2.cpp is overriding somehow the value of this variable – djfoxmccloud May 14 '12 at 15:01
  • @djfoxmccloud "file1.h I first declare it as CGraphWnd graph_wnd_sensor1D" - `CGraphWnd graph_wnd_sensor1D;` is not a declaration, but a definition. You need to declare it using `extern`. – Luchian Grigore May 14 '12 at 15:04
  • @LuchianGrigore Well in Visual if I right click from file1.cpp graph_wnd_sensor1D, both show declaration and show definition point to CGraphWnd graph_wnd_sensor1D; in file1.h. I've updated the OP for more info – djfoxmccloud May 14 '12 at 15:24
  • @djfoxmccloud you still don't have the code I posted. – Luchian Grigore May 14 '12 at 19:12