2

I am quite new to both C++ and Cppunit. I am trying to compile a little cppunit test. However, I do not succeed.

qwerty@qwerty:~/chessgame/src$ g++ -Wall CoordinateTest.cpp ftest.cpp -lcppunit -o test
/tmp/cc1vJJqz.o: In function `CoordinateTest::coordinateTest()':
CoordinateTest.cpp:(.text+0xbb): undefined reference to `Coordinate::Coordinate(int const&, int const&)'
CoordinateTest.cpp:(.text+0xe2): undefined reference to `Coordinate::Coordinate(int const&, int const&)'
collect2: ld returned 1 exit status

This is the sourcecode. What am I missing here?

----- Coordinate.h -----------------------------------------------------------------

#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate
{

    public:
        Coordinate(const int& x, const int& y);
        void setCoordinates(const int& x, const int& y);
        const int& getCoordinateX() const;
        const int& getCoordinateY() const;

    private:
        int mX;
        int mY;

};

#endif

----- Coordinate.cpp -----------------------------------------------------------------

#include "Coordinate.h"

Coordinate::Coordinate(const int& x, const int& y) : mX(x),mY(y)
{
}

void Coordinate::setCoordinates(const int& x, const int& y)
{
    mX = x;
    mY = y;
}

const int& Coordinate::getCoordinateX() const
{
    return mX;
}

const int& Coordinate::getCoordinateY() const
{
    return mY;
}

----- CoordinateTest.h -----------------------------------------------------------------

#ifndef COORDINATETEST_H
#define COORDINATETEST_H

#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include "Coordinate.h"

class CoordinateTest : public CPPUNIT_NS::TestFixture
{
    CPPUNIT_TEST_SUITE (CoordinateTest);
    CPPUNIT_TEST (coordinateTest);
    CPPUNIT_TEST_SUITE_END ();

    public:
        void setUp();
        void tearDown();

protected:
        void coordinateTest();

};

#endif

----- CoordinateTest.cpp -----------------------------------------------------------------

#include "CoordinateTest.h"

CPPUNIT_TEST_SUITE_REGISTRATION (CoordinateTest);

void CoordinateTest::setUp(void)
{

}

void CoordinateTest::tearDown(void) 
{

}

void CoordinateTest::coordinateTest(void)
{
    Coordinate from(1, 0);
    Coordinate to(2, 0);
    CPPUNIT_ASSERT_EQUAL (true, true);
}

----- ftest.cpp -----------------------------------------------------------------

#include "CoordinateTest.h"
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>

int main (int argc, char* argv[])
{
    // informs test-listener about testresults
    CPPUNIT_NS::TestResult testresult;

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener(&collectedresults);

    // register listener for per-test progress output
    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener(&progress);

    // insert test-suite at test-runner by registry
    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
    testrunner.run(testresult);

    // output results in compiler-format
    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write();

    // return 0 if tests were successful
    return collectedresults.wasSuccessful() ? 0 : 1;
}
0

2 Answers 2

2

Try adding Coordinate.cpp to your commandline.

qwerty@qwerty:~/chessgame/src$ g++ -Wall CoordinateTest.cpp ftest.cpp -lcppunit -o test

=>

qwerty@qwerty:~/chessgame/src$ g++ -Wall CoordinateTest.cpp Coordinate.cpp ftest.cpp -lcppunit -o test

0

Add coordinate.cpp to your command-line. The linker can't find the bodies of any of the Coordinate::* methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.