Bonjour,

Comment déclarer une variable globale utilisable dans plusieurs classes ?

j'ai un projet d'IHM ou j'ai besoin de variable globale (je sais c'est pas très beau)..

voici se que j'ai fait mais qui ne marche pas hihih!


main.cpp
---------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>
#include "variable.h"
#include "AfficheExterne.h"
 
using namespace std;
 
int main()
{
 
  int variableG=100;
 
  CAffExterne Call;
  Call.View();
 
   system("PAUSE");
   return EXIT_SUCCESS;
}

variable.h
-------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
#if !defined(VARIABLE_H)
#define VARIABLE_H
#pragma once
 
extern int variableG=0;
 
#endif


AfficheExterne.h
---------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#if !defined(CAFFEXTERNE_H)
#define CAFFEXTERNE_H
#pragma once
 
#include <iostream> 
#include "Variable.h"
 
 
class CAffExterne
{
 
public:
 void View();
 
};
#endif
AfficheExterne.cpp
---------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
#include "AfficheExterne.h"
 
void CAffExterne::View()
{
   std::cout<<"Variable Globale : " <<variableG <<std::endl;
}


Enfin c'est une version simplifié.

Le probleme DevCpp me marque plusieurs erreurs :
-Variable.h : [Warning] `variableG' initialized and declared `extern'
-Variable.h : multiple definition of `variableG'
first defined here
- [Linker error] undefined reference to `variableG'

Auriez vous des idées ? par avance merci.