2

我对 C++ 和 Cppunit 都很陌生。我正在尝试编译一个小 cppunit 测试。但是,我没有成功。

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

这是源代码。我在这里想念什么?

----- 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;
}
4

2 回答 2

2

尝试将 Coordinate.cpp 添加到您的命令行。

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

于 2009-06-03T19:50:11.313 回答
0

添加coordinate.cpp到您的命令行。链接器找不到任何Coordinate::*方法的主体。

于 2009-06-03T19:49:24.653 回答