0

I'm a C++ beginner, so sorry if this question is stupid.

I have three file: main.cpp, mapper.hpp and its src file mapper.cpp.

mapper.hpp declare a struct which extends ff_node, which has the pure virtual method void* svc(void* task);:

//mapper.hpp
#define HAS CXX11 VARIADIC TEMPLATES 1

#include <iostream>
#include <ff/pipeline.hpp>

using namespace std;

#ifndef MAPPER_HPP_
#define MAPPER_HPP_

template<typename S1>
struct Mapper : ff::ff_node {
public:
    void* svc(void* task);
};

#endif /* MAPPER_HPP_ */

This is mapper.cpp:

//mapper.cpp
#include "mapper.hpp"

template<typename S1>
void* Mapper<S1>::svc(void* task) {
    //do something here
}

And finally main.cpp:

#include <iostream>
#include <vector>
#include <ff/pipeline.hpp>
#include <ff/farm.hpp>

#include "mapper.hpp"
#include "splitter.hpp"

using namespace ff;

int main(int argc, char *argv[]) {
//...
    std::vector<std::unique_ptr<ff_node> > Workers;
    for(int i=0;i<nworkers;++i)
        Workers.push_back(make_unique<Mapper<int>>());
//..
}

Compiling with the following command:

g++ -std=c++11 -DNO_DEFAULT_MAPPING -I /home/luca/fastflow -O3 -finline-functions -DNDEBUG mapper.cpp Main.cpp -o Main -pthread

Returns me the following error:

/tmp/ccUY3VtR.o:(.rodata._ZTV6MapperIiE[_ZTV6MapperIiE]+0x118): undefined reference to `Mapper<int>::svc(void*)'
collect2: error: ld returned 1 exit status

What is strange is that if I declare svc(void*) insider mapper.hpp then no error is returned and everything is compiled correctly!

cpplearner
  • 13,776
  • 2
  • 47
  • 72
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • Mmmh yeah but I thought that didn't count because the template in my case is used in the struct declaration, not in the function svc declaration. – justHelloWorld Dec 02 '15 at 11:56
  • 1
    It is still exactly the issue described in the answer to that other question. The compilation of mapper.cpp has no way to know you want the code instantiated for `` and the compilation of main.cpp has no definition of `svc` – JSF Dec 02 '15 at 12:00
  • Now it's clear, thanks! – justHelloWorld Dec 02 '15 at 12:51

0 Answers0