LNK2019:VS单元测试中未解析的外部符号 [英] LNK2019: unresolved external symbol in VS unit-testing

查看:253
本文介绍了LNK2019:VS单元测试中未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到标题中所述的错误.我确保以下事项:
-包含目录,包含库和其他包含目录已正确设置
-在属性中,子系统"设置为CONSOLE

I get the error as stated in the title. I ensured the following:
- The Include directory, include library and additional include directory are set correctly
- In the properties, Subsystem is set to CONSOLE

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

Comments to my code: LifeLib is a project that contains classes of that I want to test some methods. The classes are defined in namespace LifeLib. One of them is StornoTafel. testVariables is NOT defined in any namespace.
I get the linking error 3 times, for 2 constructors and 1 method in StornoTafel (noted in the code).

//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>

详细错误:

  1. LNK2019未解析的外部符号"public:__cdecl LifeLib :: StornoTafel :: StornoTafel(void) 函数"public:"中引用(0StornoTafel @ LifeLib @@ QEAA @ XZ): __cdecl AggSelTester :: AggSelTest :: AggSelTest(void)(?? 0AggSelTest @ AggSelTester @@ QEAA @ XZ)
  2. 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)
  3. LNK2019未解决的外部 符号"public:LifeLib :: StornoTafel __cdecl类 LifeLib :: StornoTafel :: operator =(类LifeLib :: StornoTafel const&) 函数中引用的(4StornoTafel @ LifeLib @@ QEAA?AV01 @ AEBV01 @@ Z) 公共:void __cdecl AggSelTester :: AggSelTest :: init(void)" (?init @ AggSelTest @ AggSelTester @@ QEAAXXZ)
  1. LNK2019 unresolved external symbol "public: __cdecl LifeLib::StornoTafel::StornoTafel(void)" (??0StornoTafel@LifeLib@@QEAA@XZ) referenced in function "public: __cdecl AggSelTester::AggSelTest::AggSelTest(void)" (??0AggSelTest@AggSelTester@@QEAA@XZ)
  2. LNK2019 unresolved external symbol "public: __cdecl LifeLib::StornoTafel::StornoTafel(class std::vector >)" (??0StornoTafel@LifeLib@@QEAA@V?$vector@NV?$allocator@N@std@@@std@@@Z) referenced in function "public: void __cdecl AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ)
  3. LNK2019 unresolved external symbol "public: class LifeLib::StornoTafel __cdecl LifeLib::StornoTafel::operator=(class LifeLib::StornoTafel const &)" (??4StornoTafel@LifeLib@@QEAA?AV01@AEBV01@@Z) referenced in function "public: void __cdecl AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ)

它们为什么会出现?

推荐答案

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

I found the answer by myself: I have to include the .cpp file.
So #include "../LifeLib/StornoTafel.cpp" fixes the error. However, I have no idea why.

这篇关于LNK2019:VS单元测试中未解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆