I use this class with - all except for the single-page member functions that will be used in the project with several source files that are related to compilation. The type of the template is unknown and can accept any type to it. In this case, I have two source files that use the class, so the header file with the class declaration and definition has #include: ed in both source files. Then I get a "multiple definition" error in the declaration of a member function without a class template. I assume this is because it is defined twice during the linking process, since both source files have a member function definition different from the template. Imagine a non-semantic scenario below:
Note. Suppose all files are protected, and iostream is #include: ed wherever it is required.
foo.hpp
class foo { public: template <typename X> void f(X); void ff (); }; #include "foo.tpp"
foo.tpp
template <typename X> void foo::f(X val) { cout << val; } void foo::ff()
main2.cpp
#include "foo.hpp"
main.cpp
#include "foo.hpp" int main() { return 0; }
Adding the inline keyword to the function definition seems to resolve this error, although I don't want to use it because I have other non-template member functions that have the same problem, which are much larger on which are referenced in several parts of the code. Is there any working or valid way to do what I'm trying to do? Thanks in advance!
source share