gpt4 book ai didi

c++ - 使用 Visual Studio C++ 进行单元测试时链接器错误

转载 作者:行者123 更新时间:2023-12-01 23:09:45 24 4
gpt4 key购买 nike

我想使用 Visual Studio 对我的 C++ 项目进行单元测试。将项目中的文件夹添加为测试项目的包含路径后,在尝试编译测试时出现链接器错误:

Severity    Code    Description Project File    Line    Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall Piece::Piece(enum Color)" (??0Piece@@QAE@W4Color@@@Z) referenced in function "public: __thiscall Bishop::Bishop(enum Color)" (??0Bishop@@QAE@W4Color@@@Z) ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall Board::~Board(void)" (??1Board@@QAE@XZ) referenced in function "public: void __thiscall ChessPlusPlusTests::BishopTests::ValidMovesTest(void)" (?ValidMovesTest@BishopTests@ChessPlusPlusTests@@QAEXXZ) ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1
Error LNK2019 unresolved external symbol "public: void __thiscall Board::placePieceAt(class Piece * const,struct Position)" (?placePieceAt@Board@@QAEXQAVPiece@@UPosition@@@Z) referenced in function "public: void __thiscall ChessPlusPlusTests::BishopTests::ValidMovesTest(void)" (?ValidMovesTest@BishopTests@ChessPlusPlusTests@@QAEXXZ) ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1
Error LNK2001 unresolved external symbol "public: virtual class std::vector<struct Position,class std::allocator<struct Position> > __thiscall Bishop::getMovesFor(struct Position,class Board &)" (?getMovesFor@Bishop@@UAE?AV?$vector@UPosition@@V?$allocator@UPosition@@@std@@@std@@UPosition@@AAVBoard@@@Z) ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1
Error LNK2001 unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Bishop::toString(void)" (?toString@Bishop@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1
Error LNK2001 unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Bishop::toShortString(void)" (?toShortString@Bishop@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1

我的测试源代码:

#include "stdafx.h"
#include "CppUnitTest.h"
#include "Bishop.h"
#include "Board.h"
#include "TestUtils.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace ChessPlusPlusTests
{
TEST_CLASS(BishopTests)
{
public:

TEST_METHOD(ValidMovesTest)
{
// Arrange
Board board{};
Bishop *piece = new Bishop{ Color::WHITE };
Position pos{ 3,3 };
board.placePieceAt(piece, pos);

// Act
auto validPositions = piece->getMovesFor(pos, board);

// Assert
TestUtils::AssertPositions(validPositions, {
0,0,0,0,0,0,0,1,
1,0,0,0,0,0,1,0,
0,1,0,0,0,1,0,0,
0,0,1,0,1,0,0,0,
0,0,0,0,0,0,0,0,
0,0,1,0,1,0,0,0,
0,1,0,0,0,1,0,0,
1,0,0,0,0,0,1,0,
});
}

};
}

如果不添加包含路径,测试项目将无法编译,因为主项目中包含的头文件依赖于包含路径。

主项目编译得很好。

有人可以帮助我了解出了什么问题吗?

谢谢!

最佳答案

我认为所选答案不正确。测试通常应该在他们的环境中运行;因此,他们不应该访问实现 (.cpp)

<小时/>

当您在 Visual Studio (VS 2017) 上创建单独的测试项目时,您需要创建对要测试的项目的引用(右键单击测试项目 -> 添加 -> 引用 -> 勾选项目): Add a reference

如果您看到一些链接器错误,请从单元测试项目中右键单击:项目->链接器->输入->其他依赖项->编辑然后添加 .obj 文件的路径。您可以尝试类似“$(SolutionDir)ConsoleApplication1\$(IntDir)*.obj”的内容,其中 ConsoleApplication1 是目标项目名称。

Path to .obj

最初,我只想对 Cornelis 的帖子发表评论,但我不能。

关于c++ - 使用 Visual Studio C++ 进行单元测试时链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47722333/

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