gpt4 book ai didi

c++ - 单位;试图编译类来测试

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:01:32 32 4
gpt4 key购买 nike

我对 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

这是源代码。我在这里错过了什么?

----- 坐标.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;
}

最佳答案

尝试将 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

关于c++ - 单位;试图编译类来测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/946737/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com