I'm having a problem with the following scheme. If everything is in one source file, it compiles and links fine:
However, when I try to break this up into multiple files (as follows), I get multiple definition of Test::foo<>. What am I missing?Code:#include <iostream> enum Type {TYPE_A, TYPE_B}; class Test { public: template <Type N> void foo(); void a(); void b(); }; void Test::a() { std::cout << "A\n"; } void Test::b() { std::cout << "B\n"; } template <> void Test::foo<TYPE_A>() { a(); } template <> void Test::foo<TYPE_B>() { b(); } int main() { Test t; t.foo<TYPE_A>(); t.foo<TYPE_B>(); std::cin.get(); }
(foo.cpp is not compiled separately.)Code://type.h #ifndef TYPE_H #define TYPE_H enum Type {TYPE_A, TYPE_B}; #endif //test.h #ifndef TEST_H #define TEST_H #include "type.h" class Test { public: template <Type N> void foo(); void a(); void b(); }; #include "foo.cpp" #endif //test.cpp #include "test.h" #include <iostream> void Test::a() { std::cout << "A\n"; } void Test::b() { std::cout << "B\n"; } //foo.cpp template <> void Test::foo<TYPE_A>() { a(); } template <> void Test::foo<TYPE_B>() { b(); } //main.cpp #include "test.h" #include "type.h" #include <iostream> int main() { Test t; t.foo<TYPE_A>(); t.foo<TYPE_B>(); std::cin.get(); }



LinkBack URL
About LinkBacks


