2011-10-23 56 views
1
我在這2個錯誤

,而編寫本C++和Visual Studio 2010專業代碼:Visual Studio 2010中:C++:錯誤LNK2001:解析外部符號

Principal.obj : error LNK2001: unresolved external symbol "public: enum TP1::Couleur __thiscall TP1::Labyrinthe::trouveGagnant(void)" ([email protected]@[email protected]@[email protected]@XZ) 

Principal.obj : error LNK2001: unresolved external symbol "public: int __thiscall TP1::Labyrinthe::solutionner(enum TP1::Couleur)" ([email protected]@[email protected]@[email protected]@@Z) 

都在.cpp和.h文件如下:

// principal.cpp 

\#include "Labyrinthe.h" 

using namespace std; 
using namespace TP1; 

int main() 
{ 
     try 
     { 
       Labyrinthe lab; 

       ifstream entree("FichiersLabyrinthe/rouge.txt"); 

       if (!entree) 
       { 
         cout << "Fichier rouge introuvable.\n"; 
         return 1; 
       } 

       lab.chargeLabyrinthe(Rouge, entree); 
       cout << "\nLabyrinthe rouge charg�.\n"; 
       entree.close(); 

       entree.open("FichiersLabyrinthe/vert.txt", ios::in); 
       if (!entree) 
       { 
         cout << "Fichier vert introuvable.\n"; 
         return 1; 
       } 

       lab.chargeLabyrinthe(Vert, entree); 
       cout << "\nLabyrinthe vert charg�.\n"; 
       entree.close(); 

       entree.open("FichiersLabyrinthe/bleu.txt", ios::in); 
       if (!entree) 
       { 
         cout << "Fichier bleu introuvable.\n"; 
         return 1; 
       } 

       lab.chargeLabyrinthe(Bleu, entree); 
       cout << "\nLabyrinthe bleu charg�.\n"; 
       entree.close(); 

       entree.open("FichiersLabyrinthe/jaune.txt", ios::in); 
       if (!entree) 
       { 
         cout << "Fichier jaune introuvable.\n\n"; 
         return 1; 
       } 

       lab.chargeLabyrinthe(Jaune, entree); 
       cout << "\nLabyrinthe jaune charg�.\n"; 
       entree.close(); 

       cout << "\nLe joueur rouge peut solutionner le labyrinthe en " 
           << lab.solutionner(Rouge) << " d�placements.\n"; 
       cout << "\nLe joueur vert peut solutionner le labyrinthe en " 
           << lab.solutionner(Vert) << " d�placements.\n"; 
       cout << "\nLe joueur bleu peut solutionner le labyrinthe en " 
           << lab.solutionner(Bleu) << " d�placements.\n"; 
       cout << "\nLe joueur jaune peut solutionner le labyrinthe en " 
           << lab.solutionner(Jaune) << " d�placements.\n"; 

       Couleur LeGagnant = lab.trouveGagnant(); 
       switch (LeGagnant) 
       { 
       case 0: 
         cout << endl << "Le joureur gagnant: Rouge" << endl << endl; 
         break; 
       case 1: 
         cout << endl << "Le joureur gagnant: Vert" << endl << endl; 
         break; 
       case 2: 
         cout << endl << "Le joureur gagnant: Bleu" << endl << endl; 
         break; 
       case 3: 
         cout << endl << "Le joureur gagnant: Jaune" << endl << endl; 
         break; 
       default: 
         cout << endl << "Le joureur gagnant: aucun!!" << endl << endl; 
         break; 
       } 
     } catch (exception & e) 
     { 
       cerr << e.what() << endl; 
     } 

     return 0; 
} 

// ====================================== ========== Labyrinthe.h

#ifndef LABYRINTHE_H_ 
#define LABYRINTHE_H_ 

#include <stdexcept> 
#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 

#include "Chemin.h" 
#include "Porte.h" 
#include "Piece.h" 
#include "FilePieces.h" 

#pragma warning(disable : 4290) 

namespace TP1 
{ 


class Labyrinthe 
{ 
public: 

     Labyrinthe(); 
     virtual ~Labyrinthe(); 
     Labyrinthe(const Labyrinthe&); 

     const Labyrinthe& operator =(const Labyrinthe& source) throw (std::bad_alloc); 

     void chargeLabyrinthe(Couleur couleur, std::ifstream &entree); 

     void ajoutePieceLabyrinthe(Piece &p) throw (std::bad_alloc); 

     int solutionner(Couleur joueur); 

     Couleur trouveGagnant(); 

     Chemin cheminLabyrinthe(Couleur joueur); 

private: 

     void ajoutePassage(Couleur couleur, int i1, int j1, int i2, int j2); 

     void placeDepart(std::string& nom) throw (std::logic_error); 

     void placeArrivee(std::string& nom) throw (std::logic_error); 


     class NoeudListePieces 
     public: 

       Piece piece; 

       NoeudListePieces *suivant; 
     }; 

     NoeudListePieces *trouvePiece(std::string &nom) const 
         throw (std::invalid_argument, std::logic_error); 
     NoeudListePieces *dernier; 

     Piece *depart, *arrivee; 

     int compteurLab; 

}; 

} 

#endif /* LABYRINTHE_H_ */ 

任何人都可以幫助我嗎?謝謝。

回答

2

添加您的業務這裏沒有提供代碼(定義)Labyrinthe::trouveGagnantLabyrinthe::solutionner。編譯器抱怨你正在調用兩個沒有在任何地方定義過的函數,所以不可能爲它生成代碼。

您忘了在項目中包含Labyrinthe.cpp嗎?

2

您只提供編碼爲Labyrinthe.hprincipal.cpp

在Labyrinthe.h中聲明的函數沒有在任何地方定義。最有可能的是,他們的定義在Labyrinthe.cpp。確保你編譯該文件。

通常,編譯器只查找符號聲明。這是因爲定義可以駐留在一個單獨的模塊中。當你鏈接到一個庫時,你會在那裏找到定義。在你的情況下,定義駐留在同一個模塊中,所以你需要編譯實現(cpp文件中的部分)。

相關問題