运行 C++ 单元测试时LNK2005链接错误

LNK2005 linkage error when running c++ unit test

本文关键字:链接 错误 LNK2005 C++ 单元测试 运行      更新时间:2023-10-16

我对c ++相对较新,我不确定如何解决此错误。我有一个命名空间QueryUtils我将其包含在另一个类QuerySyntaxProcessing中。以下是以下定义的定义

查询实用程序.cpp

#pragma once
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
namespace QueryUtils
{
static const std::string SELECT_KEYWORD = "Select" + ' ';
static const std::string SUCHTHAT_KEYWORD = ' ' + "Such that";
size_t split(const std::string& txt, std::vector<std::string>& strs, char ch)
{
size_t pos = txt.find(ch);
size_t initialPos = 0;
strs.clear();
// Decompose statement
while (pos != std::string::npos) {
strs.push_back(txt.substr(initialPos, pos - initialPos));
initialPos = pos + 1;
pos = txt.find(ch, initialPos);
}
// Add the last one
strs.push_back(txt.substr(initialPos, std::min(pos, txt.size()) - initialPos + 1));
return strs.size();
}
std::string getStringBetween(const std::string& s, const std::string& start_delim, const std::string& stop_delim)
{
unsigned first_delim_pos = s.find(start_delim);
unsigned end_pos_of_first_delim = first_delim_pos + start_delim.length();
unsigned last_delim_pos = s.find(stop_delim);
return s.substr(end_pos_of_first_delim,
last_delim_pos - end_pos_of_first_delim);
}
};

查询语法处理.cpp

#include "QuerySyntaxProcessing.h"
#include <ctype.h>
#include <string>
#include <vector>
#include <iostream>
#include <map>
#include "QueryUtils.cpp"
QuerySyntaxProcessing::QuerySyntaxProcessing()
{
entityMap["stmt"] = DesignEntities::NORMAL_STATEMENT;
entityMap["variable"] = DesignEntities::VARIABLE; 
designEntities = { "stmt", "read", "print", "call", "while", "if", "assign", "variable", "constant", "procedure" };
}
QuerySyntaxProcessing::~QuerySyntaxProcessing()
{
}

由于方法太长,我在QuerySyntaxProcessing.cpp省略了更多详细信息。但是,我在此类的函数中使用了QueryUtils的命名空间方法。我在构建项目时没有任何错误。

查询语法测试.cpp

#include "stdafx.h"
#include <vector>
#include <string>
#include "QuerySyntaxProcessing.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTesting
{
TEST_CLASS(TestPQLSytnax)
{
public:
TEST_METHOD(SynonymTest) {
QuerySyntaxProcessing* queryProcessing = new QuerySyntaxProcessing();
vector<string> multipleVars = { "v", "a", "cd" };
vector<string> singleVars = { "hello" };
vector<string> wrongVars = { "54v" };
Assert::AreEqual(queryProcessing->isSynonym(multipleVars), true);
Assert::AreEqual(queryProcessing->isSynonym(singleVars), true);
Assert::AreEqual(queryProcessing->isSynonym(wrongVars), false);
}
};
}

当我尝试为QuerySyntaxProcessing.cpp构建单元测试时,我得到一个错误LNK 2005。

甚至尝试初始化实例的构造函数时*QuerySyntaxProcessing会出现错误消息。我没有在任何其他标题中包含QueryUtils.cpp标题。所以不太确定出了什么问题。

Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl QueryUtils::getStringBetween(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?getStringBetween@QueryUtils@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@00@Z) already defined in QueryUtilsTest.obj UnitTesting C:Usersyichoteam16-win-spa-19s2Team00Code00UnitTesting\SPA.lib(QuerySyntaxProcessing.obj( 1

谢谢大家的帮助..

就我而言,您不应该有单元测试标头的相应.cpp文件。在你的情况下,它是QuerySyntaxProcessing.cpp,你不应该拥有它。所以所有的实现都应该写在.h中(用现代C++标准并不难(。

我想在TEST_CLASS、TEST_METHOD或类似的东西中定义了一些全局或静态变量。