Bonjour,
Je me suis mis au C++ deux jours et je rencontre une difficulté plutôt frustrante:
J'ai trois fichiers: main.cpp Mot.cpp Mot.hpp. Voici le code:
main.cpp :
Mot.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
19
20
21 #include <fstream> #include "Mot.hpp" #include <string> using namespace std; int main() { cout << "Saisir un mot :" << endl; string chaine; cin >> chaine; Mot mot; mot.setChaine(chaine); mot.melanger(); cout << "Les lettres sont : " << mot.getMelange() << endl; cout << "Proposition : " << endl; cin >> chaine; if(chaine.compare(mot.getChaine()) == 0) cout << "OK !" << endl; else cout << "Non !" << endl; return 0; }
Mot.hpp :
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 #include <iostream> #include <string> #include <cstdlib> #include "Mot.hpp" using namespace std; class Mot { private: string chaine; string melange; public: Mot() { } Mot(string _chaine) { chaine = _chaine; } void melanger() { unsigned short int i(0),j; while(i < chaine.length()) { j = (rand()*(chaine.length())); melange[j] = chaine[i]; i++; } } string getMelange() { return melange; } string getChaine() { return chaine; } void setChaine(string _chaine) { chaine = _chaine; } virtual ~Mot() { } }
A la ligne 12 dans main.cpp j'obtiens l'erreur suivante: undefined reference to Mot::Mot()
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
19 #ifndef MOT_HPP_INCLUDED #define MOT_HPP_INCLUDED #include <string> using namespace std; class Mot { public: Mot(); Mot(string _chaine); void melanger(); string getMelange(); void setChaine(string _chaine); string getChaine(); virtual ~Mot(); }; #endif // MOT_HPP_INCLUDED
Dans les différents tutoriels que j'ai consulté, la manière de créer un objet semble être identique, et je ne vois pas d'erreur dans les fichiers Mot.cpp et Mot.hpp. Peut-être une étourderie de ma part ?
Je vous remercie d'avance pour toute réponse.
Ben
Partager