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

时间:2020-02-01 11:26:11

标签: c++ unit-testing

我对C ++还是比较陌生,我不确定如何解决此错误。我有一个命名空间QueryUtils,正在将它包含到另一个类QuerySyntaxProcessing中。以下是以下

的定义

QueryUtils.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);
}
};

QuerySyntaxProcessing.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的命名空间方法。构建项目时,我没有任何错误。

QuerySyntaxTests.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:\Users\yicho\team16-win-spa-19s2\Team00\Code00\UnitTest ing \ SPA.lib(QuerySyntaxProcessing.obj)1

谢谢大家的帮助。

0 个答案:

没有答案
相关问题