Question

I've got two classes : EtatTrafic and EtatTraficTest.

This is my makefile :

#Makefile
CXX = g++
LDLIBS = -lcppunit

# a modifier en fonction des cas
OBJS = EtatTrafic.o EtatTraficTest.o 

all : TestUnitaire

TestUnitaire: $(OBJS)
    $(CXX) $^ -o $@ $(LDFLAGS) $(LDLIBS)

#création des objets
%.o : %.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<


# a modifier en fonction des cas
EtatTrafic.o: EtatTrafic.hpp 
EtatTraficTest.o: EtatTrafic.hpp EtatTraficTest.hpp 

clean:
    rm *.o $(EXEC)

When i try to compile I get some errors like :

EtatTraficTest.o: In function `EtatTraficTest::testConstructeur()':
EtatTraficTest.cpp:(.text+0xab): undefined reference to `EtatTrafic::EtatTrafic(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
EtatTraficTest.cpp:(.text+0xeb): undefined reference to `EtatTrafic::EtatTrafic(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
EtatTraficTest.cpp:(.text+0x169): undefined reference to `EtatTrafic::getNbVoitures() const'
EtatTraficTest.cpp:(.text+0x256): undefined reference to `EtatTrafic::getNbPietons() const'

It seems to be that there is no link between my classes...

I you want, these are the .hpp and .cpp of EtatTrafic :

//EtatTrafic.hpp
    #ifndef ETATTRAFIC_HPP_
#define ETATTRAFIC_HPP_


using namespace std;

/**
 * @class EtatTrafic
 * @brief Gestion des etats du trafic
 * @author Fasy
 * @author Pothier
 * @author Duval
 * @author Lupascu
 * @author Mira
 * @version 1.0
 * @date avril 2013
 */
class EtatTrafic {

    private :
    unsigned int nbPietons;
    unsigned int nbVoitures;
    unsigned int nbCamions;
    unsigned int nbMotos;
    unsigned int nbVelos;
    unsigned int nbAccident;

public :

    /**
     * @fn EtatTrafic(unsigned int nbPietons,unsigned int nbVoitures,unsigned int nbCamions,unsigned int nbMotos, unsigned int nbVelos)
     * @brief Constructeur paramétré
     * @param nbPietons : nombre de pietons presents dans la ville
     * @param nbVoitures : nombre de voitures presentes dans la ville
     * @param nbCamions : nombre de camions presents dans la ville
     * @param nbMotos :     nombre de motos presents dans la ville
     * @param nbVelos : nombre de velos presents dans la ville
     */
    EtatTrafic(unsigned int nbPietons,unsigned int nbVoitures,unsigned int nbCamions,unsigned int nbMotos, unsigned int nbVelos);

    /**
     * @fn unsigned int calculNbVehicules()
     * @brief calcul du nombre total de vehicules existants
     */
    unsigned int calculNbVehicules();
    /**
     * @fn unsigned int getNbAccidents()
     * @brief renvoie le nombre d'accidents
     */
    unsigned int getNbAccidents();

    /**
     * @fn unsigned int setNbAccidents()
     * @brief change le nombre d'accidents
     */
    unsigned int setNbAccidents();

    /**
     * @fn void setNbVoitures(unsigned int nbVt)
     * @brief changer le nombre de voitures dans la ville
     * @param nbVt :le nouveau nombre de voitures
     */
        void setNbVoitures(unsigned int nbVt);
        /**
         * @fn void setNbPietons(unsigned int nbP)
         * @brief changer le nombre de pietons dans la ville
         * @param nbVt :le nouveau nombre de pietons
         */
        void setNbPietons(unsigned int nbP);
        /**
         * @fn void setNbCamions(unsigned int nbC)
         * @brief changer le nombre de camions dans la ville
         * @param nbVt :le nouveau nombre de camions
         */
        void setNbCamions(unsigned int nbC);
        /**
         * @fn void setNbVelos(unsigned int nbVl)
         * @brief changer le nombre de Velos dans la ville
         * @param nbVt :le nouveau nombre de velos
         */
         void setNbVelos(unsigned int nbM);

         /**
          * @fn void setNbMotos(unsigned int nbM)
          * @brief changer le nombre de motos dans la ville
          * @param nbM :le nouveau nombre de motos
          */
         void setNbMotos(unsigned int nbM);
         /**
          * @fn unsigned int getNbVoitures() const
          * @brief Obtenir le nobre de voitures de la ville
          */
         unsigned int getNbVoitures() const;
         /**
          * @fn unsigned int getNbPietons() const
          * @brief Obtenir le nobre de pietons de la ville
          */
         unsigned int getNbPietons() const;
         /**
          * @fn unsigned int getNbCamions() const
          * @brief Obtenir le nobre de camions de la ville
          */

         unsigned int getNbCamions() const;
         /**
          * @fn unsigned int getNbVelos() const
          * @brief Obtenir le nobre de velos de la ville
          */
         unsigned int getNbVelos() const;

         /**
          * @fn unsigned int getNbMotos() const
          * @brief Obtenir le nobre de motos de la ville
          */
         unsigned int getNbMotos() const;


};



#endif /* ETATTRAFIC_HPP_ */

    // EtatTrafic.cpp

#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>


int main(int argc, char* argv[])
{
  // Get the top level suite from the registry
  // déclaration d'une suite de tests
  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

  // Adds the test to the list of test to run
  // chargement de la suite de tests présentée dans la classe ...Test.hpp
  CppUnit::TextUi::TestRunner runner;
  runner.addTest( suite );

  // Change the default outputter to a compiler error format outputter
  // transformation du format de sortie
  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
                                                        std::cerr ) );
  // Run the tests.
  // lancement des tests
  bool wasSucessful = runner.run();

  // Return error code 1 if the one of test failed.
  // retour
  return wasSucessful ? 0 : 1;
}

These are the .hpp and .cpp of EtatTraficTest :

//EtatTrafictest.chpp
#ifndef ETATTRAFICTEST_H
#define ETATTRAFICTEST_H

//ligne a recopier
#include <cppunit/extensions/HelperMacros.h>

class EtatTraficTest : public CppUnit::TestFixture
{
//ligen a recopier (change le "MoneyTest")
 CPPUNIT_TEST_SUITE( EtatTraficTest );

 //ordre d'execution des tests
  CPPUNIT_TEST( testConstructeur );
  CPPUNIT_TEST( testCalculNbVehicules );

  //ligen a recopier
  CPPUNIT_TEST_SUITE_END();

public:

//déclaration des méthodes tests
  void testConstructeur();
  void testCalculNbVehicules();
  void testCalculNbAccident();

};

#endif

// EtatTraficTest.cpp
//include à appliquer sur nos classes
#include "EtatTraficTest.hpp"
#include "EtatTrafic.hpp"

//ligne à recopier tel quel
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( EtatTraficTest );

//déclaration des méthodes tests 
// attention ces méthodes ne contiennent pas de paramètres !
void  EtatTraficTest::testConstructeur()
{
  // Set up
  // déclation des objets qui vont etre utilisés
  const unsigned int nbPietons0 =0;
  const unsigned int nbPietons1 =3;
  //const unsigned int nbPietons2 = ;
  const unsigned int nbVoitures0 =0;
  const unsigned int nbVoitures1 =2;  
  //const unsigned int nbVoitures2 = ;  
  const unsigned int nbCamions0 =0;  
  const unsigned int nbCamions1 =5;    
  //const unsigned int nbCamions2 = ;    
  const unsigned int nbMotos0 =0;    
  const unsigned int nbMotos1 =6;      
  //const unsigned int nbMotos2 = ;      
  const unsigned int nbVelos0 =0;   
  const unsigned int nbVelos1 =4;
  //const unsigned int nbVelos2 = ;


  // Process
  // appel de la méthode à tester (ici il s'agit du constructeur)

  //cas limite
   EtatTrafic etat0(nbPietons0, nbVoitures0, nbCamions0, nbMotos0, nbVelos0);

   //cas normal
   EtatTrafic etat1(nbPietons1, nbVoitures1, nbCamions1, nbMotos1, nbVelos1);

   //cas d'erreur
   //EtatTrafic etat2(nbPietons2, nbVoitures2, nbCamions2, nbMotos2, nbVelos2);


  // Check
  // lancement des tests unitaires 

  CPPUNIT_ASSERT_EQUAL( nbVoitures0, etat0.getNbVoitures() );
  CPPUNIT_ASSERT_EQUAL( nbPietons0, etat0.getNbPietons() );
  CPPUNIT_ASSERT_EQUAL( nbCamions0, etat0.getNbCamions() );
  CPPUNIT_ASSERT_EQUAL( nbMotos0, etat0.getNbMotos() );
  CPPUNIT_ASSERT_EQUAL( nbVelos0, etat0.getNbVelos() );

  CPPUNIT_ASSERT_EQUAL( nbVoitures1, etat1.getNbVoitures() );
  CPPUNIT_ASSERT_EQUAL( nbPietons1, etat1.getNbPietons() );
  CPPUNIT_ASSERT_EQUAL( nbCamions1, etat1.getNbCamions() );
  CPPUNIT_ASSERT_EQUAL( nbMotos1, etat1.getNbMotos() );
  CPPUNIT_ASSERT_EQUAL( nbVelos1, etat1.getNbVelos() );

  /*CPPUNIT_ASSERT_EQUAL( nbVoitures2, etat2.getNbVoitures() );
  CPPUNIT_ASSERT_EQUAL( nbPietons2, etat2.getNbPietons() );
  CPPUNIT_ASSERT_EQUAL( nbCamions2, etat2.getNbCamions() );
  CPPUNIT_ASSERT_EQUAL( nbMotos2, etat2.getNbMotos() );
  CPPUNIT_ASSERT_EQUAL( nbVelos2, etat2.getNbVelos() );*/

}
void  EtatTraficTest::testCalculNbVehicules()
{
  // Set up
  // déclation des objets qui vont etre utilisés
  const unsigned int nbPietons0 =0;
  const unsigned int nbPietons1 =3;
  //const unsigned int nbPietons2 = ;
  const unsigned int nbVoitures0 =0;
  const unsigned int nbVoitures1 =2;  
  //const unsigned int nbVoitures2 = ;  
  const unsigned int nbCamions0 =0;  
  const unsigned int nbCamions1 =5;    
  //const unsigned int nbCamions2 = ;    
  const unsigned int nbMotos0 =0;    
  const unsigned int nbMotos1 =6;      
  //const unsigned int nbMotos2 = ;      
  const unsigned int nbVelos0 =0;   
  const unsigned int nbVelos1 =4;
  //const unsigned int nbVelos2 = ;

const unsigned int nbVehiculeTotale0= nbPietons0+nbVoitures0+nbCamions0+nbMotos0+nbVelos0;
const unsigned int nbVehiculeTotale1= nbPietons1+nbVoitures1+nbCamions1+nbMotos1+nbVelos1;
//const unsigned int nbVehiculeTotale2= nbPietons2+nbVoitures2+nbCamions2+nbMotos2+nbVelos2;
 // Process
  // appel de la méthode à tester (ici il s'agit du constructeur)

  //cas limite
   EtatTrafic etat0(nbPietons0, nbVoitures0, nbCamions0, nbMotos0, nbVelos0);

   //cas normal
   EtatTrafic etat1(nbPietons1, nbVoitures1, nbCamions1, nbMotos1, nbVelos1);

   //cas d'erreur
   //EtatTrafic etat2(nbPietons2, nbVoitures2, nbCamions2, nbMotos2, nbVelos2);

   // Check
  // lancement des tests unitaires 

  CPPUNIT_ASSERT_EQUAL(nbVehiculeTotale0, etat0.calculNbVehicules() );
  CPPUNIT_ASSERT_EQUAL( nbVehiculeTotale1, etat1.calculNbVehicules() );
  //CPPUNIT_ASSERT_EQUAL( nbVehiculeTotale2, etat2.calculNbVehicules() );

  }
Was it helpful?

Solution

Your compiler complains that it cannot find definitions for members of EtatTrafic, such as EtatTrafic::getNbVoitures(). This method is declared in EtatTrafic.hpp (the file says that the method exists), but it is not defined in any of the files you have shown us (no file says what the method does).

By convention, the definition of methods of EtatTrafic should be in EtatTrafic.cpp, but they are not. If they do not exist, you must write them; if they exist in some other file, you must either move them to EtatTrafic.cpp, or add the appropriate object file to the makefile variable OBJS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top