Erreur class has not been declared
Résolu
Bonjour à tous,
je suis en train de faire un petit exercice en c++ et je rencontre un problème qui m'empêche de terminer. voici le code :
//fichier ProfDeGym.h
//fichier TourDePiste.h
//fichier ProfDeGym.cpp
//fichier TourDePiste.cpp
//fichier main.cpp
quand je compile le main sous ubuntu, j'ai les erreurs suivantes:
velocity@velocity-laptop:~/Documents/proj1$ gcc -o main.o main.cpp
In file included from main.cpp:2:
ProfDeGym.h:11: error: `TourDePiste' has not been declared
ProfDeGym.h:15: error: variable or field `tourFini' declared void
ProfDeGym.h:15: error: `TourDePiste' was not declared in this scope
ProfDeGym.h:15: error: `tour' was not declared in this scope
main.cpp: In function `int main()':
main.cpp:6: error: `TourDePiste' was not declared in this scope
main.cpp:6: error: expected `;' before `tour'
main.cpp:9: error: `tour' was not declared in this scope
main.cpp:11: error: expected unqualified-id before `.' token
main.cpp:14: error: `cout' was not declared in this scope
main.cpp:14: error: expected primary-expression before `<<' token
merci d'avance,
0
je suis en train de faire un petit exercice en c++ et je rencontre un problème qui m'empêche de terminer. voici le code :
//fichier ProfDeGym.h
#ifndef H_ProfDeGym
#define H_ProfDeGym
using namespace std;
#include "TourDePiste.h"
class ProfDeGym{
public :
void tourFini(TourDePiste&);
};
void ProfDeGym::tourFini(TourDePiste& tour){
if(tour.nbTours>0)
tour.nbTours--;
}
#endif
//fichier TourDePiste.h
#ifndef H_TourDePiste
#define H_TourDePiste
using namespace std;
class TourDepiste{
int getTours(){return nbTours;}
void setTours(int & nbTours){this->nbTours=nbTours;}
friend void tourFini();
private:
int nbTours;
};
#endif
//fichier ProfDeGym.cpp
#include <ProfDeGym.h>
//fichier TourDePiste.cpp
#include <TourDePiste.h>
//fichier main.cpp
#include "TourDePiste.h"
#include "ProfDeGym.h"
int main(){
TourDePiste tour = new TourDePiste();
ProfDeGym();
tour.setTours(10);
for(int i=0;i<5;i++){
ProfDeGym.TourFini(tour);
}
cout << "nbTour" << tour.getTours(); << "\n";
return 0;
}
quand je compile le main sous ubuntu, j'ai les erreurs suivantes:
velocity@velocity-laptop:~/Documents/proj1$ gcc -o main.o main.cpp
In file included from main.cpp:2:
ProfDeGym.h:11: error: `TourDePiste' has not been declared
ProfDeGym.h:15: error: variable or field `tourFini' declared void
ProfDeGym.h:15: error: `TourDePiste' was not declared in this scope
ProfDeGym.h:15: error: `tour' was not declared in this scope
main.cpp: In function `int main()':
main.cpp:6: error: `TourDePiste' was not declared in this scope
main.cpp:6: error: expected `;' before `tour'
main.cpp:9: error: `tour' was not declared in this scope
main.cpp:11: error: expected unqualified-id before `.' token
main.cpp:14: error: `cout' was not declared in this scope
main.cpp:14: error: expected primary-expression before `<<' token
merci d'avance,
0
8 réponses
-
Bonjour,
Je te donne la solution ci-dessous, qui reprend ton code et tient principalement en deux mots : "forward declaration".
Ça permet de se servir d'un nom de classe dans une déclaration sans avoir inclu le h.
Par contre, pour faire marcher j'ai du modifier des déclarations qu'il faut que tu potasse un peu, si je puis me permettre
Les deux lignes du main qui déclaraient tout et ProfDeGym montrent que tu ne maîtrise pas le sujet des déclarations. Cherche des tuto d'initiation C++ et tu y gagneras
dernier point : ce code mple et fonctionne sous g++ (testé !)//Le cde de velocituy remanié qui est compilable et fonctionne correctement en compilant avec g++ //fichier main.cpp // J'ai rajouté include iostream (pour cout) #include <iostream> #include "TourDePiste.h" // L'inclde ProfDegym n'est pas utile car déjà inclu dans TourDePiste.h, mais ça ne gêne pas #include "ProfDeGym.h" int main(){ TourDePiste tour; // pas de parentheses après tour ProfDeGym profdeGym; // ici tu n'avais rien déclaré en faisant simplement ProfDeGym() tour.setTours(10); for(int i=0;i<5;i++){ profdeGym.tourFini(tour); // attention au majuscules minuscules } std::cout << "nbTour : " << tour.getTours() << '\n'; return 0; } //fichier TourDePiste.h // je n'est rien changé das ce fichier, je le remet pour mémoire #ifndef H_TourDePiste #define H_TourDePiste #include "ProfDeGym.h" class TourDePiste { public: unsigned int getTours() const {return nbTours;} void setTours(const unsigned int n){nbTours=n;} friend void ProfDeGym::tourFini(TourDePiste&); private: unsigned int nbTours; }; #endif //fichier ProfDeGym.h #ifndef H_ProfDeGym #define H_ProfDeGym // **** C'est la la modif la plus importante : il ne faut pas include le .h de TourDePiste sinon redondance cyclique // La ligne ci-dessous se nomme une "forward declaration", ça permet de gérer les références croisées class TourDePiste; class ProfDeGym { public : void tourFini(TourDePiste &lesToursDePiste); }; #endif //fichier ProfDeGym.cpp // Ici, par contre, on a beoin de la "vraie" déclaration de TourDePiste, il faut donc l'include "TourDePiste.h" // J'ai donc juste rajouté cette ligne. #include "ProfDeGym.h" #include "TourDePiste.h" void ProfDeGym::tourFini(TourDePiste& ptour) { if(ptour.nbTours>0) ptour.nbTours--; }
Cordialement-
ModérateurJe suis d'accord pour l'utilisation du friend comme cela, j'avoue avoir été un peu réticent à l'utiliser, car en général si on peut s'en passer (et là on le pouvait) autant l'oublier...
Mais dans la mesure où ici c'était imposé il n'y a plus vraiment le choix ^^
Cependant, il faudrait quand même remettre les mots-clés inline pour getTours et setTours sinon les fonctions en questions ne seront pas inline alors que c'est ce qu'on attend d'elles...
ProfDeGym.h#ifndef H_ProfDeGym #define H_ProfDeGym class TourDePiste; class ProfDeGym { public : void tourFini(TourDePiste &tour); }; #endif
TourDePiste.h#ifndef H_TourDePiste #define H_TourDePiste #include "ProfDeGym.h" class TourDePiste { private: friend void tourFini(TourDePiste &tour); unsigned int nbTours; public: inline unsigned int getTours() const { return nbTours; } inline void setTours(const unsigned int n) { nbTours=n; } friend void ProfDeGym::tourFini(TourDePiste &tour); }; #endif
ProfDeGym.cpp#include "ProfDeGym.h" #include "TourDePiste.h" void ProfDeGym::tourFini(TourDePiste &tour) { if (tour.nbTours>0) tour.nbTours--; }
Test.cpp#include <iostream> #include "TourDePiste.h" #include "ProfDeGym.h" int main() { TourDePiste tour; tour.setTours(10); ProfDeGym prof; for (unsigned int i=0; i<5; i++) prof.tourFini(tour); std::cout << "nbTour = " << tour.getTours() << std::endl; return 0; }
De plus je trouverais plus cohérent de définir un constructeur pour la classe TourDePiste de manière à attribuer une valeur initiale à nbTours (0 par exemple) car pour l'instant on s'oblige à utiliser setTours juste après le constructeur, alors que si on l'oublie getTours renverrait n'importe quoi ! -
Merci beaucoup pour votre réponse java4ever, j'ai essayé votre code avec Dev C++ et ca marche enfin, merci pour l'explication du forward declaration et le temps que vous a pris pour m'ecrire le code.
C'est vrai je ne suis pas très à l'aise avec le C++ et j'en ai un cours qui explique un peut les nouveau concepts et j'en apprend petit à petit tout en essayant les exercices.
Maintenant je peut passe à l'exercice suivant .
merci à tous pour votre aide .
-