0

I have created a simple cpp project in Visual Studio, but have encountered the follwoing linking error:

LNK2019: unresolved external symbol "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

I have no idea what may be causing this and would be gratefull for any advice. Files that constitute the project:

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));
}

solver.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);
};

solver.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);
 }
MarkusSPE
  • 81
  • 1
  • 10
  • 1
    `solver.cpp` contains implementation code for the generic *template*, but no specializations thereof. See [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – dxiv Jun 14 '20 at 20:58

1 Answers1

0

You need to include vector library to use std::vector.

#include <vector>

"Unresolved symbol" means that there is a class or a function declared but not found anywhere in the project (or in the libraries it includes).