Ste*_*eve 5 c++ unit-testing linker-errors lnk2019 visual-studio
我收到标题中所述的错误。我确保以下各项:
-正确设置了Include目录,include库和其他include目录
-在属性中,Subsystem设置为CONSOLE
对我的代码的注释:LifeLib是一个项目,其中包含我要测试某些方法的类。这些类在命名空间LifeLib中定义。StornoTafel就是其中之一。在任何名称空间中均未定义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>
Run Code Online (Sandbox Code Playgroud)
错误详情:
- 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 @@@函数“ public:void __cdecl AggSelTester :: AggSelTest :: init(void)”中引用的std @@@ Z)(?init @ AggSelTest @ AggSelTester @@ QEAAXXZ)
- LNK2019无法解析的外部符号“ public:类LifeLib :: StornoTafel __cdecl LifeLib :: StornoTafel :: operator =(类LifeLib :: StornoTafel const&)”(?? 4StornoTafel @ LifeLib @@@@@@ QEAA?AV01 @ AEBV01 @@ Z)函数“ public:void __cdecl AggSelTester :: AggSelTest :: init(void)”(?init @ AggSelTest @ AggSelTester @@ QEAAXXZ)
为什么会出现?
Lia*_*iam 10
我知道这已经很晚了,但让我解释一下为什么包含 .cpp 文件可以解决这个问题。
#include "../LifeLib/StornoTafel.h"
...
LifeLib::StornoTafel stornoTafel_; // Throws unresolved external symbol
Run Code Online (Sandbox Code Playgroud)
您的单元测试项目存在于您的应用程序之外,您提供了对头文件的引用,这些头文件提供了您的类型和方法的声明,但没有提供它们的定义。
通过放置对 .cpp 文件的引用,编译器实际上可以获得这些签名的声明:
#include "../LifeLib/StornoTafel.h"
#include "../LifeLib/StornoTafel.cpp"
...
LifeLib::StornoTafel stornoTafel_; // Works perfectly
Run Code Online (Sandbox Code Playgroud)
最简单的解决方案是简单地复制头文件引用并将 .h 更改为 .cpp
小智 1
根据我的经验,有两件事会导致此错误出现,要么在您使用的软件中没有正确地将其包含在您的项目中,因此出现链接错误,要么您的项目和项目的版本之间可能存在混合您尝试包含的那个。我的意思是,检查它们是否都是 64 或 32。如果它们不相同,则会显示此错误。我知道这些是可能导致此问题的原因,也可能是其他原因。
| 归档时间: |
|
| 查看次数: |
3061 次 |
| 最近记录: |