什么会导致 Visual Studio 中的 LNK2019 错误?
Posted
技术标签:
【中文标题】什么会导致 Visual Studio 中的 LNK2019 错误?【英文标题】:What can cause LNK2019 error in VisualStudio? 【发布时间】:2020-06-14 19:21:56 【问题描述】:我在 Visual Studio 中创建了一个简单的 cpp 项目,但遇到了以下链接错误:
LNK2019: 未解析的外部符号“public: class std::vector > __thiscall solver::euler(class point_3d (__cdecl*)(class point_3d,double),double,int,class point_3d,double)” (?euler@ ?$solver@Vpoint_3d@@@@QAE?AV?$vector@Vpoint_3d@@V?$allocator@Vpoint_3d@@@std@@@std@@P6A?AVpoint_3d@@V4@N@ZNH0N@Z
我不知道是什么原因造成的,如果有任何建议,我将不胜感激。 构成项目的文件:
main.cpp
#include "point_3d.h"
#include "solver.h"
point_3d lorentz(point_3d val, double t)
double x = 10*(val.get_y()-val.get_x());
double y = val.get_x()*(28.0- val.get_z())- val.get_y();
double z = val.get_x()* val.get_y()-(8.0/3)* val.get_z();
return point_3d(x, y, z);
int main()
auto file = std::ofstream("data/results.json");
solver<point_3d> sol;
std::vector<point_3d> result = sol.euler(lorentz, 0.01, 50000, point_3d(1, 1, 1));
求解器.h
template<typename T>
class solver
public:
std::vector<T> euler(T (*f)(T val, double s),double h, int steps, T val, double t0 = 0.0);
std::vector<T> back_euler(T (*f)(T val, double s), double h, int steps, T val, double t0 = 0.0,int n=50);
std::vector<T> rk_2(T (*f)(T val, double s), double h, int steps, T val, double t0 = 0.0);
std::vector<T> rk_4(T (*f)(T val, double s), double h, int steps, T val, double t0 = 0.0);
;
求解器.cpp
#include "solver.h"
template<typename T>
std::vector<T> solver<T>::euler(T (*f)(T val, double s), double h, int steps, T val, double t0 )
std::vector<T>res;
res.resize(steps + 1);
res[0] = val;
double t = t0;
for (int i = 0; i < steps;i++)
res[i + 1] = res[i] + h * f(res[i], t);
t += h;
return res;
template<typename T>
std::vector<T> solver<T>::back_euler(T (*f)(T val, double s), double h, int steps, T val, double t0, int n)
std::vector<T>res;
res.resize(steps + 1);
res[0] = val;
double t = t0;
for (int i = 0; i < steps; i++)
t += h;
res[i + 1] = res[i];
for (int j = 0; j < n; j++)
res[i + 1] += res[i] + h * f(res[i + 1],t);
return res;
template<typename T>
std::vector<T> solver<T>::rk_2(T (*f)(T val, double s), double h, int steps, T val, double t0)
std::vector<T>res;
res.resize(steps + 1);
res[0] = val;
double t = t0;
for (int i = 0; i < steps; i++)
auto k = h * f(res[i], t) / 2.0;
res[i + 1] = res[i] + h * f(k+res[i],t+h/2.0);
t += h;
return res;
template<typename T>
std::vector<T> solver<T>::rk_4(T (*f)(T val, double s), double h, int steps, T val, double t0)
std::vector<T>res;
res.resize(steps + 1);
res[0] = val;
double t = t0;
for (int i = 0; i < steps; i++)
auto k1 = f(res[i],t);
auto k2 = f(res[i]+h/2.0*k1, t+h/3.0);
auto k3 = f(res[i] + h / 2.0 * k2, t + 2*h / 3.0);
auto k4 = f(res[i] + h * k3, t + h);
res[i + 1] = res[i] + (k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6) * h;
t += h;
return res;
point_3d.h
class point_3d
double x;
double y;
double z;
public:
point_3d(double x, double y, double z): x(x), y(y), z(z) ;
double get_x();
double get_y();
double get_z();
point_3d operator+(point_3d other);
point_3d& operator=(point_3d other);
friend point_3d operator*(double k, point_3d p);
;
point_3d.cpp
#include "point_3d.h"
double point_3d::get_x()
return x;
double point_3d::get_y()
return y;
double point_3d::get_z()
return z;
point_3d point_3d::operator+(point_3d other)
double x = get_x() + other.get_x();
double y = get_y() + other.get_y();
double z = get_z() + other.get_z();
return point_3d(x, y, z);
point_3d& point_3d::operator=(point_3d other)
this->x = other.get_x();
this->y = other.get_y();
this->z = other.get_z();
return *this;
point_3d operator*(double k, point_3d p)
double x = p.get_x();
double y = p.get_y();
double z = p.get_z();
return point_3d(k * x, k * y, k * z);
【问题讨论】:
solver.cpp
包含通用 template 的实现代码,但没有其特化。见Why can templates only be implemented in the header file?。
【参考方案1】:
您需要包含向量库才能使用 std::vector。
#include <vector>
“未解析的符号”表示有一个类或函数已声明但在项目的任何地方(或它包含的库中)都找不到。
【讨论】:
以上是关于什么会导致 Visual Studio 中的 LNK2019 错误?的主要内容,如果未能解决你的问题,请参考以下文章
向任何控件添加事件处理程序会导致 Visual Studio 中的错误
是啥导致 Visual Studio 2017 中的文件名为空
使用 Visual Studio 2019 对 BizTalk 2020 人工制品进行单元测试会导致 Intellisense 问题