Home > Blockchain >  Template class specialization in seperate .cpp file
Template class specialization in seperate .cpp file

Time:11-25

I have the following template class specializations in foo.h:

using VisionMethod = enum
{
    TemplateMatching,
    ...
};

template <VisionMethod>
class Foo
{
    Foo() = delete;
};

template <>
class Foo<TemplateMatching>
{
public:
    static void get_pose();
};

and wish to implement it in foo.cpp as:

#include "foo.h"

void
Foo<TemplateMatching>::get_pose()
{
    std::cout << "Using foo<TemplateMatching>::get_pose()...\n";
}

But I (somewhat understandably) get a linker error when I try to call foo<TemplateMatching>::get_pose() in e.g. main.cpp after only including foo.h. Can I achieve what I want, without having to include foo.cpp in either foo.h or main.cpp - maybe using explicit instantiation? Or is something like foo.txx my only "clean" solution?

CodePudding user response:

You need to define your specialized function (i.e what you put in foo.cpp) in foo.h in order to use it in main.cpp

CodePudding user response:

what you proposed already works (after add #include <iostream>), you probably forget to tell the compiler to include foo.cpp.

like g main.cpp foo.cpp

https://wandbox.org/permlink/LDpxhgeYPkakWFwE

  • Related