首页 文章

LNK2019:VS单元测试中未解析的外部符号

提问于
浏览
4

我收到 Headers 中所述的错误 . 我确保了以下内容:

  • 正确设置包含目录,包含库和其他包含目录
  • 在属性中,Subsystem设置为CONSOLE

对我的代码的评论:LifeLib是一个项目,包含我想测试一些方法的类 . 这些类在命名空间LifeLib中定义 . 其中之一是StornoTafel . testVariables未在任何名称空间中定义 .
我得到链接错误3次,对于StornoTafel中的2个构造函数和1个方法(在代码中注明) .

//project Tester
#include "stdafx.h"
#include "CppUnitTest.h"

#include "../LifeLib/StornoTafel.h"
#include "../LifeLib/testVariables.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace Tester
{       
    TEST_CLASS(AggSelTest)
    {
    public:
        LifeLib::StornoTafel stornoTafel_; // LNK2019
        LifeLib::StornoTafel *stornoTafel_; // no error, but I need an instance and not a reference to proceed -> see init method
        LifeLib::testVariables test_vars_; // everything is fine

        TEST_METHOD_INITIALIZE(init) {
            stornoTafel_ = StornoTafel(test_vars_.lapseProb); // when this line is commented out I only get the first error (see below)
        }
    }
}

// testVariables.h
#pragma once
#include <iostream>
#include <vector>

class testVariables {
public:
    testVariables() {};
// here are a lot of vectors with values for testing purposes
std::vector<double> _lapseProb= {0,1,2}; // [...]
};

// StornoTafel.h
#pragma once
#include "masterheader.h"

namespace LifeLib {
    class StornoTafel {
    public:

        StornoTafel(); //LNK2019
        StornoTafel(std::vector<double> ProbabilityOfLapseInYearT); //LNK2019

        StornoTafel(const StornoTafel &obj); //no error

        StornoTafel operator=(StornoTafel const& rhs); //LNK2019

        //! \name Getter
        //@{ 
        const std::vector<double>& Stornowahrscheinlichkeit() const;
        //@}
    protected:
        std::vector<double> Stornowahrscheinlichkeit_;
    };
    inline const std::vector<double>& StornoTafel::Stornowahrscheinlichkeit() const {
        return Stornowahrscheinlichkeit_;
    }
}

//StornoTafel.cpp
#include "StornoTafel.h"

LifeLib::StornoTafel::StornoTafel() {
}

LifeLib::StornoTafel::StornoTafel(std::vector<double> ProbabilityOfLapseInYearT) {
    Stornowahrscheinlichkeit_ = ProbabilityOfLapseInYearT;
}

LifeLib::StornoTafel::StornoTafel(const StornoTafel &obj) {
    Stornowahrscheinlichkeit_ = obj.Stornowahrscheinlichkeit_;
}

LifeLib::StornoTafel LifeLib::StornoTafel::operator=(StornoTafel const& rhs) {
    Stornowahrscheinlichkeit_ = rhs.Stornowahrscheinlichkeit_;
    return *this;
}

//masterheader.h
#pragma once
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

#include <algorithm>
#include <ctime>

错误详情:

LNK2019未解析的外部符号“public:__ cdecl LifeLib :: StornoTafel :: StornoTafel(void)”(?? 0StornoTafel @ LifeLib @@ QEAA @ XZ)在函数“public:cdecl AggSelTester :: AggSelTest :: AggSelTest(void)”中引用(?? 0AggSelTest @ AggSelTester @@ QEAA @XZ)LNK2019未解析的外部符号“public: cdecl LifeLib :: StornoTafel :: StornoTafel(class std :: vector>)”(?? 0StornoTafel @LifeLib @@ QEAA @ V?$ vector @NV?$ allocator @ N @ std @@@ std @@@ Z)在函数“public:void __cdecl AggSelTester :: AggSelTest :: init(void)”中引用(?init @ AggSelTest @ AggSelTester @@ QEAAXXZ)LNK2019未解决外部符号“public:class LifeLib :: StornoTafel __cdecl LifeLib :: StornoTafel :: operator =(class LifeLib :: StornoTafel const&)”(?? 4StornoTafel @ LifeLib @@ QEAA?AV01 @ AEBV01 @@ Z)在函数中引用“ public:void __cdecl AggSelTester :: AggSelTest :: init(void)“(?init @ AggSelTest @ AggSelTester @@ QEAAXXZ)

它们为什么会出现?

4 回答

  • 1

    我自己找到了答案:我必须包含.cpp文件 .
    所以 #include "../LifeLib/StornoTafel.cpp" 修复了错误 . 但是,我不知道为什么 .

  • 1

    根据我的经验,有两件事情会出现这个错误,无论是在你使用的软件中你没有正确地将它包含在你的项目中,因此出现链接错误,或者你的项目和版本之间可能存在混合 . 你试图包括的那个 . 我的意思是,检查它们是64还是32.如果它们不相同,则会出现此错误 . 这些是我所知道的可能导致这种情况的东西,它可能是其他东西 .

  • 3

    我知道这已经很晚了,但让我解释为什么包含.cpp文件可以解决问题 .

    #include "../LifeLib/StornoTafel.h"
    
    ...
    
    LifeLib::StornoTafel stornoTafel_; // Throws unresolved external symbol
    

    您的单元测试项目存在于您的应用程序之外,您提供了对头文件的引用,这些头文件提供了 declarations 类型和方法,但没有提供 definitions .

    通过放置对.cpp文件的引用,编译器实际上可以获得这些签名的声明:

    #include "../LifeLib/StornoTafel.h"
    #include "../LifeLib/StornoTafel.cpp"
    
    ...
    
    LifeLib::StornoTafel stornoTafel_; // Works perfectly
    

    最简单的解决方案是简单地复制头文件引用并将.h更改为.cpp

  • 0

    我有类似的问题,并通过将输出.obj文件添加到测试项目的依赖项来修复它 .

    参考MSDN文档 - https://msdn.microsoft.com/en-us/library/hh419385.aspx#objectRef

相关问题