简体   繁体   中英

Visual Studio 2010: C++: Error LNK2001: unresolved external symbol

I'm having these 2 errors, while compiling this C++ code with Visual Studio 2010 professional:

Principal.obj : error LNK2001: unresolved external symbol "public: enum TP1::Couleur __thiscall TP1::Labyrinthe::trouveGagnant(void)" (?trouveGagnant@Labyrinthe@TP1@@QAE?AW4Couleur@2@XZ)

Principal.obj : error LNK2001: unresolved external symbol "public: int __thiscall TP1::Labyrinthe::solutionner(enum TP1::Couleur)" (?solutionner@Labyrinthe@TP1@@QAEHW4Couleur@2@@Z)

Both the .cpp and the .h files are below:

// 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_ */

Anyone can help me please? Thanks.

You have not given the code (definition for) methods Labyrinthe::trouveGagnant and Labyrinthe::solutionner . The compiler is complaining that you are calling two functions that have not been defined anywhere, so it's not possible to produce code for it.

Did you forget to include Labyrinthe.cpp in your project?

You only supplied code for Labyrinthe.h and principal.cpp .

The functions declared in Labyrinthe.h are not defined anywhere. Most likely, their definition resides in Labyrinthe.cpp . Make sure you compile that file as well.

In general, the compiler only looks for symbol declarations. That is because definitions can reside in a separate module. When you link against a library, you find the definition there. In your case, the definition resides in the same module, so you need the implementation to be compiled (the part in the cpp file).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM