F
F
Flaker2015-09-04 16:46:44
C++ / C#
Flaker, 2015-09-04 16:46:44

How to fix template error LNK2019?

The error itself:

1>main.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Furdarius::MatrixHelpers::inputFromFile<int>(class Furdarius::Matrix<int> &,class std::basic_fstream<char,struct std::char_traits<char> > &)" ([email protected]@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@@[email protected]@@Z) referenced in function "class Furdarius::Matrix<int> __cdecl initMatrixFromFile(class std::basic_fstream<char,struct std::char_traits<char> > &)" ([email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@Z)

The code that generates it:
// MatrixHelpers.h
class MatrixHelpers
    {
    public:
        template<typename _U>
        static void inputFromFile(Matrix<_U> &matrix, std::fstream &file);

// MatrixHelpers.cpp
template<typename _U>
    void MatrixHelpers::inputFromFile(Matrix<_U> &matrix, std::fstream &file)
    {
        for (int i = 0; i < matrix.getSize().row; ++i)
        for (int j = 0; j < matrix.getSize().col; ++j)
          file >> matrix[i][j];
    };

Method call in main.cpp
Matrix<int> M(size);
MatrixHelpers::inputFromFile<int>(M, file);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2015-09-04
@Flaker

Template functions cannot be described in separate cpp files. Write the body of the method directly in the h file.

D
Dmitry Kovalsky, 2015-09-04
@dmitryKovalskiy

Yes, just in case. Is there a ";" after the class{} closing brace?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question