Search code examples
c++linkerlnk2019

Linker error when using OutputIterator in a static library


I tried to search for similar problems but didn't find a thread which could help me solve my problem. Sorry in advance if I missed it!

I am pretty new to C++ even if I have experience with other languages such as Java and I am using Visual Studio Express 2015. So, I have a class which performs some set computations. The idea of using an output iterator comes from this SO topic from which I copied the structure of the code.

The header looks like this:

#pragma once
#include <tuple>
#include <vector>
#include <iterator>

using namespace std;

class SomeComputation
{
public:
    SomeComputation(void);
    ~SomeComputation(void);


    template <typename Range1, typename Range2, typename Range3, typename OutputIterator>
    void compute(Range1 const & r1, Range2 const &r2, Range3 const & r3, OutputIterator out);
};

The implementation looks like this:

#include "SomeComputation.h"
using namespace std;

SomeComputation::SomeComputation(void)
{
// build the object
}

SomeComputation::~SomeComputation(void)
{
// destroy it
}
template <typename Range1, typename Range2, typename Range3, typename OutputIterator>
void SomeComputation::compute(Range1 const & r1, Range2 const &r2, Range3 const & r3, OutputIterator out) {

// do some set computation here
// then save the result using the output iterator
*out++ = std::make_tuple([OMISSIS]);
}

Now, the class is packed in a static library which is correctly linked to another project, in which I call it this way:

#include <vector>
#include <tuple>
#include <iterator>
#include <SomeComputation.h>

using namespace std;
    void MyClass::myFunction(){
    // init data (please assume that 
    //minQ1, maxQ1, etc. are double vars correctly initialized)
        vector<double> rQ0q1;
        rQ0q1.push_back(minQ1);
        rQ0q1.push_back(maxQ1);
        vector<double> rQ0q2;
        rQ0q2.push_back(minQ2);
        rQ0q2.push_back(maxQ2);
        vector<double> rQ0r3;
        rQ0r3.push_back(minR3);
        rQ0r3.push_back(maxR3);
        SomeComputation s = SomeComputation();
        vector<tuple<double, double, double>> result;
        s.compute(rQ0q1, rQ0q2, rQ0r3, std::back_inserter(result));
    }

However, I get the following linker error LNK2019:

Error   LNK2019 unresolved external symbol "public: void __thiscall SomeComputation::compute<class std::vector<double,class std::allocator<double> >,class std::vector<double,class std::allocator<double> >,class std::vector<double,class std::allocator<double> >,class std::back_insert_iterator<class std::vector<class std::tuple<double,double,double>,class std::allocator<class std::tuple<double,double,double> > > > >(class std::vector<double,class std::allocator<double> > const &,class std::vector<double,class std::allocator<double> > const &,class std::vector<double,class std::allocator<double> > const &,class std::back_insert_iterator<class std::vector<class std::tuple<double,double,double>,class std::allocator<class std::tuple<double,double,double> > > >)" (??$compute@V?$vector@NV?$allocator@N@std@@@std@@V12@V12@V?$back_insert_iterator@V?$vector@V?$tuple@NNN@std@@V?$allocator@V?$tuple@NNN@std@@@2@@std@@@2@@SomeComputation@@QAEXABV?$vector@NV?$allocator@N@std@@@std@@00V?$back_insert_iterator@V?$vector@V?$tuple@NNN@std@@V?$allocator@V?$tuple@NNN@std@@@2@@std@@@2@@Z) referenced in function "public: void __thiscall ModelData::addDataAtFrame(int,class Vector,class Vector)" (?addDataAtFrame@ModelData@@QAEXHVVector@@0@Z)   [path and project name omitted] 1

Now, although I can solve the error by using a "more traditional" return value, I don't understand what's causing it and why it is happening. I say that the library is correctly linked because I can use all the other functions/objects/classes of the library without any problem, but if I use this one I get the error. Can anyone please help me clarifying it?

Thanks a lot.


Solution

  • Define your template function in the header file not the source file: Why can templates only be implemented in the header file?

    We are all at this point at some time^^