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

2024-01-02

我收到标题中所述的错误。我保证了以下几点:
- 包含目录、包含库和附加包含目录设置正确
- 在属性中,子系统设置为 CONSOLE

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

//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)" (??0StornoTafel@LifeLib@@QEAA@XZ) 在函数“public: __cdecl AggSelTester::AggSelTest::AggSelTest(void)" (??0AggSelTest@AggSelTester@@QEAA@XZ)
  2. LNK2019 未解决的外部 符号“公共:__cdecl LifeLib :: StornoTafel :: StornoTafel(类 std::向量 >)" (??0StornoTafel@LifeLib@@QEAA@V?$vector@NV?$allocator@N@std@@@std@@@Z) 在函数“public: void __cdecl”中引用 AggSelTester::AggSelTest::init(void)" (?init@AggSelTest@AggSelTester@@QEAAXXZ)
  3. LNK2019 未解决的外部 符号“公共:类 LifeLib::StornoTafel __cdecl LifeLib::StornoTafel::operator=(类 LifeLib::StornoTafel const &)" (??4StornoTafel@LifeLib@@QEAA?AV01@AEBV01@@Z) 在函数中引用 “公共:无效__cdecl AggSelTester :: AggSelTest :: init(无效)” (?init@AggSelTest@AggSelTester@@QEAAXXZ)

它们为什么会出现?


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐