LNK2005运行c+时出现联动错误+;单元测试 我对C++是比较新的,我不确定如何解决这个错误。我有一个名称空间QueryUtils,我正在将它包含到另一个类QuerySyntaxProcessing中。以下是以下各项的定义:

LNK2005运行c+时出现联动错误+;单元测试 我对C++是比较新的,我不确定如何解决这个错误。我有一个名称空间QueryUtils,我正在将它包含到另一个类QuerySyntaxProcessing中。以下是以下各项的定义:,c++,unit-testing,C++,Unit Testing,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_KEYWOR

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
构建单元测试时,我得到一个错误LNK2005

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

错误LNK2005“class std::basic_string\u cdecl QueryUtils::getStringBetween(class std::basic_string const&,class std::basic_string const&,class std::basic_string const&)”(?getStringBetween@QueryUtils@@是吗?AV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@ABV23@00@Z)已在QueryUtilsTest.obj单元测试C:\Users\YCHO\team16-win-spa-19s2\Team00\Code00\UnitTest中定义


谢谢大家的帮助。

QueryUtils.cpp
不是作为头文件编写的,不应该包含在任何地方。那么我应该将我的命名空间实现放在一个.h文件中,而不是?@calveeen但是请记住,头文件中的函数声明,它们的定义(实现)是一个源文件。除非将函数
设为静态
内联
@Someprogrammerdude,我明白了。所以我必须创建两个文件,一个.h文件包含名称空间的声明,另一个.cpp文件包含实现的声明?在这种情况下,我应该在哪里定义常量静态变量?@Someprogrammerdude嘿,我已经知道了,谢谢!
#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()
{
}
#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);

        }

    };
}