При вынесении методов класса в отдельный файл выдает ошибку LNK2019

У меня есть проект, содержащий следующие файлы Matrix.cpp, src/Vector.h, src/Vector.cpp

При попытке собрать проект, выскакивает ошибка

Matrix.cpp.obj : error LNK2019: ссылка на неразрешенный внешний символ "public: __cdecl num::Vector<class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > >::Vector<class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > >(int,int)" (??0?$Vector@V?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@@num@@QEAA@HH@Z) в функции main.
Matrix.cpp.obj : error LNK2019: ссылка на неразрешенный внешний символ "public: __cdecl num::Vector<class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > >::~Vector<class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > >(void)" (??1?$Vector@V?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@@num@@QEAA@XZ) в функции main.
Matrix.cpp.obj : error LNK2019: ссылка на неразрешенный внешний символ "public: void __cdecl num::Vector<class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > >::display(void)" (?display@?$Vector@V?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@@num@@QEAAXXZ) в функции main.
Matrix.cpp.obj : error LNK2019: ссылка на неразрешенный внешний символ "public: class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > __cdecl num::Vector<class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > >::create(class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > >)" (?create@?$Vector@V?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@@num@@QEAA?AV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@V34@@Z) в функции main.
matrix.exe : fatal error LNK1120: неразрешенных внешних элементов: 4

Я пытаюсь исправить эту ошибку почти месяц, прошерстил очень много сайтов, но так и не нашел решения. Все, что я понял, так это что компилятор активно игнорирует реализацию методов в другом файле. Потому что, когда я копирую все методы класса в один файл, например vector.h или даже matrix.cpp,то код работает как и должен.

Файл Matrix.cpp

#include <iostream>
#include <vector>

#include "src/Vector.h"
using namespace std;

int main(){

    typedef vector<vector<int>> newVector;
    typedef num::Vector<newVector> Vect;

    Vect matr1(2, 4);
    Vect matr2(2,4);

    newVector a1 = {{2, 4, 5, 8}, {4, 8, 0, 0}};
    newVector a2 = {{5, 8, 1, 0}, {9, 7, 1, 3}};

    matr1.create(a1);
    matr2.create(a2);
    matr1.display();
    matr2.display();
}

Файл src/Vector.cpp:

#include "Vector.h"

#include <iostream>
#include <vector>

using namespace std;
using num::Vector;
// column, row
template<typename Number>
inline Vector<Number>::Vector(int c, int r)
{
    if ((c < 0) || (r < 0)) {
        cerr << "Wrong size vectors";
    }
    else {
        column = c;
        row = r;
        vector<int>buff;
        for (auto i = 0; i < c; i++) {
            for (auto j = 0; j < r; j++) {
                buff.push_back(0);
            }
            matrix.push_back(buff);
            buff.clear();
        }
    }
}

template<typename Number>
inline Vector<Number>::~Vector()
{
    matrix.clear();
}

template <typename Number>
inline int Vector<Number>::getRow() {
    return row;
}

template <typename Number>
inline int Vector<Number>::getColumn() {
    return column;
}

template<typename Number>
void Vector<Number>::display()
{
    for (int i = 0; i < getColumn(); i++) {
        for (int j = 0; j < getRow(); j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

template<typename Number>
inline bool Vector<Number>::isCheckSize(Number vec) {
    if (getColumn() == vec.size()) {
        for (int i = 0; i < getColumn(); i++) {
            if (vec[i].size() != getRow()) return false;
        }
        return true;
    }
    return false;
}

template<typename Number>
inline Number Vector<Number>::create(Number useMatrix)
{
    
    if (isCheckSize(useMatrix)) {


        for (auto i = 0; i < getColumn(); i++) {
            for (auto j = 0; j < getRow(); j++) {
                matrix[i][j] = useMatrix[i][j];
            }
        }
        return matrix;
    }
    else {
        cerr << "Wrong size!" << endl << "The created vectors with " <<
            endl << "column: " << column << "\rrow: " << row << endl;
        throw invalid_argument("The size doens't fit!");
    }
}

template<typename Number>
Number Vector<Number>::dot(Vector matrixTwo)
{
    return Number();
}

template<typename Number>
Number Vector<Number>::operator+(Vector& n2)
{
    if (isCheckSize(n2)) {
        Vector<Number> buffer(column, row);
        for (int i = 0; i < getColumn(); i++) {
            for (int j = 0; j < getRow(); j++) {
                buffer[i][j] = matrix[i][j] + n2[i][j];
            }
        }
        return buffer;
    }
    else {
        throw invalid_argument("Wrong size matrix");
    }
}

template<typename Number>
Number Vector<Number>::operator=(Vector& n1)
{
    if ((n1.getRow() == row) && (n1.getColumn == column)) {
        for (int i = 0; i < getColumn(); i++) {
            for (int j = 0; j < getRow(); j++) {
                matrix[i][j] = n1[i][j];
            }
        }
        return matrix;
    }
    else {
        throw invalid_argument("Wrong size matrix");
    }
}

src/Vector.h

#ifndef VECTOR_H
#define VECTOR_H

namespace num {

    template <typename Number>
    class Vector
    {
    private:
        int column, row;
    public:

        Number matrix;
        Vector(int c, int r);   // column, row
        //Vector(Number& vec);
        ~Vector();

        // boolean function
        bool isCheckSize(Number vec);

        // print vector
        void display();

        //// getters
        int getRow();
        int getColumn();

        // Create new vector or matrix
        Number create(Number useMatrix);

        /* Mathematical operations with vectors */
        Number dot(Vector matrixTwo);

        //Number vec(Vector matr);

        Number operator+ (Vector& n2);
        //Vector operator== (Vector& n1);
        Number operator= (Vector& n1);
    };
}

#endif // !VECTOR_H


Ответы (0 шт):