I'm trying to pass value of anonymous enum to class method. It is defined in code that I depend on and cannot change. Enum is defined inside a structure, in form as in code below. I tried to use template method, and it works, but only if both main, class definition, and method definition are in one file. Why? I use C++11. Here is my approach:
mcve.cpp:
#include "B.h"
typedef struct {
enum {
OPTION1,
OPTION2
} some_enum;
} some_struct;
class A
{
public:
template<class functType> int my_method(functType type);
};
template<class functType> int A::my_method(functType type)
{
switch (type)
{
case some_struct::OPTION1:
return 0;
case some_struct::OPTION2:
return 1;
default:
return 2;
}
return 0;
}
template<class functType> int B::my_method(functType type)
{
return 0;
}
int main(void)
{
int ret;
A a;
ret = a.my_method(some_struct::OPTION1);
B b;
ret = b.my_method(some_struct::OPTION1);
return ret;
}
B.h:
class B
{
public:
template<class functType> int my_method(functType type);
};
This works, but when I move following part to separate file:
template<class functType> int B::my_method(functType type)
{
return 0;
}
I get linker error:
<linker_path>/ld: <project_path>/src/mcve.cpp:43: undefined reference to `int B::my_method<some_struct::{unnamed type#1}>(some_struct::{unnamed type#1})'
I use Eclipse and it's built in build system.
Aucun commentaire:
Enregistrer un commentaire