Hello,

When i compile i get theses undefiend reference to extern lobal variable. In my code, i try to do an file
that define it and extern it on SystemeBataille.cpp call th definitor on main.cpp but same error. If i place my extern on an specific function exemple SystemeBataille::placeExternHere i get an not declared error. First question: Do my file for defined variables is useful and do something wrong? Second Question: Do as a beginner i'm better to place all in same file with one and only header with variables as protected? and Thirth Question: There an way for define and extern it correctly? If that only links how i get it linked with Code::Blocks with MnGW? and last one: If i use extern on header file as i see on web i get an storage class problem

Sorry if variable and functions name are in french i'm not primary english user

Here my codes:

SystemeBataille.cpp

#include "Personnage.h"
#include "Ennemis.h"
#include "LevelUp.h"
#include "SystemeBataille.h"
#include "Barbare.h"
#include "Magicien.h"
#include "InitVariable.h"
#include <iostream>
#include <string>

using namespace std;


extern int m_StatsVieActuelPerso;
    extern int m_StatsVieMaxPerso;
    extern int m_StatsVitPerso;
    extern int m_StatsMpActuelPerso;
    extern int m_StatsMpMaxPerso;
    extern int m_StatsStrPerso;
    extern int m_StatsRstPerso;
    extern int m_StatsBaseDmgPerso;
    extern int vieRegenerer;
    extern int magieRegenerer;
    extern int pointsExperience;
    extern std::string barbare;
    extern std::string sorcier;
    extern std::string pasteur;
    extern std::string classeDuJoueur;
    extern std::string nomEnnemi;
extern int statsVieMaxEnnemi;
extern int statsVieActuelEnnemi;
extern int statsMpActuelEnnemi;
extern int statsStrEnnemi;
extern int statsRstEnnemi;
extern int statsVitEnnemi;
extern int statsBaseDmgEnnemi;


SystemeBataille::SystemeBataille()
{
}

void SystemeBataille::roundePreparation()
{
    std::string transfererNomEnnemi = nomEnnemi;
    transfererVieMaxEnnemi = statsVieMaxEnnemi;
    transfererVieActuelEnnemi = statsVieActuelEnnemi;
    transfererMpEnnemi = statsMpActuelEnnemi;
    transfererForceEnnemi = statsStrEnnemi;
    transfererResistanceEnnemi = statsRstEnnemi;
    transfererVitesseEnnemi = statsVitEnnemi;
    transfererDegatsEnnemi = transfererDegatsEnnemi;
    transfererDegatBaseEnnemi = statsBaseDmgEnnemi;
}

void SystemeBataille::determinerTour()
{
    if (statsVitEnnemi > m_StatsVitPerso)
    {
        tourDecider = 1;
    }

    else (statsVitEnnemi == m_StatsVitPerso);
    {
        tourDecider = 0;
    }

    if (m_StatsVitPerso > statsVitEnnemi)
    {
        tourDecider = 2;
    }
}


void SystemeBataille::aNommer()
{

}

SystemeBataille.h

#ifndef SYSTEMEBATAILLE_H_INCLUDED
#define SYSTEMEBATAILLE_H_INCLUDED

#include <iostream>
#include <string>
#include "Personnage.h"
#include "Ennemis.h"

class SystemeBataille
{
public:
SystemeBataille();
void roundePreparation();
void aNommer();
void determinerTour();

protected:
std::string transfererNomEnnemi;
int transfererVieMaxEnnemi;
int transfererVieActuelEnnemi;
int transfererMpEnnemi;
int transfererForceEnnemi;
int transfererResistanceEnnemi;
int transfererVitesseEnnemi;
int transfererDegatsEnnemi;
int transfererDegatBaseEnnemi;
int tourDecider;

};


#endif // SYSTEMEBATAILLE_H_INCLUDED

InitVariable.cpp

#include "Personnage.h"
#include "InitVariable.h"
#include "Ennemis.h"
#include "LevelUp.h"
#include "SystemeBataille.h"
#include "Barbare.h"
#include "Magicien.h"
#include "UtiliserObjet.h"


InitVariable::InitVariable()
{

}

void InitVariable::InitialiserVariable()
{

    int    m_StatsVieActuelPerso = 0;
int m_StatsVieMaxPerso = 0;
 int m_StatsVitPerso = 0;
int m_StatsMpActuelPerso = 0;
int m_StatsMpMaxPerso = 0;
int m_StatsStrPerso = 0;
int m_StatsRstPerso = 0;
int m_StatsBaseDmgPerso = 0;
int vieRegenerer = 0;
int magieRegenerer = 0;
int pointsExperience = 0;
std::string barbare;
std::string sorcier;
std::string pasteur;
 std::string classeDuJoueur;
std::string nomEnnemi;
int statsVieMaxEnnemi = 0;
 int statsVieActuelEnnemi = 0;
int statsMpActuelEnnemi = 0;
int statsStrEnnemi = 0;
int statsRstEnnemi = 0;
int statsVitEnnemi = 0;
int statsBaseDmgEnnemi = 0;
}

InitVariable.h

#ifndef INITVARIABLE_H_INCLUDED
#define INITVARIABLE_H_INCLUDED

#include "Personnage.h"
#include "Ennemis.h"
#include "SystemeBataille.h"

#include "InitVariable.h"

class InitVariable
{
    public:

  InitVariable();
void  InitialiserVariable();

};
#endif // INITVARIABLE_H_INCLUDED

and finally main.cpp

#include <iostream>
#include <string>
#include "Personnage.h"
#include "Ennemis.h"
#include "Barbare.h"
#include "Magicien.h"
#include "LevelUp.h"
#include "UtiliserObjet.h"
#include "SystemeBataille.h"
#include "InitVariable.h"


using namespace std;


int main()
{
InitVariable initvar;
initvar.InitialiserVariable();
 return 0;
}

Thanks i nadvance

Recommended Answers

All 7 Replies

You forgot to post Personnage.h so we might not be able to help you troubleshoot your problem. Can you at least tell us which variables it's complaining about being undefined?

sorry i forgot:

Personnage.h

#ifndef PERSONNAGE_H_INCLUDED
#define PERSONNAGE_H_INCLUDED

#include <iostream>
#include <string>

class Personnage
{
public:
    Personnage();
    Personnage(std::string nom);

    void recevoirDegats(int degats);
    void coupDePoing(Personnage &cible) const;
    void siEnVie();

protected:
int m_StatsVieActuelPerso;
    int m_StatsVieMaxPerso;
    int m_StatsVitPerso;
    int m_StatsMpActuelPerso;
    int m_StatsMpMaxPerso;
    int m_StatsStrPerso;
    int m_StatsRstPerso;
    int m_StatsBaseDmgPerso;
    int vieRegenerer;
    int magieRegenerer;
    int pointsExperience;
    std::string barbare;
    std::string sorcier;
    std::string pasteur;
    std::string classeDuJoueur;
    };

#endif

Ennemis.h

#ifndef ENNEMIS_H_INCLUDED
#define ENNEMIS_H_INCLUDED

#include <iostream>
#include <string>

#include "SystemeBataille.h"
#include "Ennemis.h"

using namespace std;

class Ennemis
{

public:
Ennemis();
void ennemiAImplanter();

protected:
std::string nomEnnemi;
int statsVieMaxEnnemi;
int statsVieActuelEnnemi;
int statsMpMaxEnnemi;
int statsMpActuelEnnemi;
int statsStrEnnemi;
int statsRstEnnemi;
int statsVitEnnemi;
int statsBaseDmgEnnemi;

};


#endif // ENNEMIS_H_INCLUDED

the error is simply undefined reference ||=== Console application, default ===|
.objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference to nomEnnemi'| .objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference tostatsVieMaxEnnemi'|
.objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference to statsVieActuelEnnemi'| .objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference tostatsMpActuelEnnemi'|
.objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference to statsStrEnnemi'| .objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference tostatsRstEnnemi'|
.objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference to statsVitEnnemi'| .objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference tostatsBaseDmgEnnemi'|
.objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference to statsVitEnnemi'| .objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference tom_StatsVitPerso'|
.objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference to m_StatsVitPerso'| .objs\SystemeBataille.o:SystemeBataille.cpp|| undefined reference tostatsVitEnnemi'|
||=== Build finished: 12 errors, 0 warnings ===|

again excuse me for forget one file

You're declaring all these variable as extern, but they don't seem to actually exist anywhere. When you say extern std::string nomEnnemi;, for example, you're not actually declaring a variable, but simply promising the compiler that it exists in another file.

You actually do declare these variables, but you only do it in a scope that's local to the InitVariable::InitialiserVariable() function. Those variables don't actually exist outside of this function. Moving these declarations outside of the function will certainly solve some of your problems.

Once you get past this problem, there will be a few other errors, particularly where you are trying to access member data that belongs to the Personnage class in a function that belongs to the SystemeBattaille class, but I don't actually know what your code is supposed to do, so I'll leave you to figure those problems out.

that work with no error! thank you so much Tumlee!

last question: do the variables will be always initianized at 0 each time ill do changes on it?
Exemple: my variable "statsRstEnnemi" is changed to 55. Do it's will pass on LevelUp each time to set it at 0? perpetually?

Your variables will only be reset to zero if you set them to zero yourself, or if you call a function that sets those variables to zero. I don't know what your "LevelUp" functions do, because you never provided them in this thread, but they shouldn't really affect the variables being passed to them unless you're passing-by-reference instead of by value.

it's only the level up system. It's use same variables than others and somes numbers willl changes whatever the file it's in

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.