I've been developing a library of mostly template functions and managed to keep things organized (to some extent) in the following manner:
// MyLib.h
class MyLib
{
template<class T>
static void Func1()
{
}
template<class T>
static void Func2()
{
}
};
And obviously calls would be made like this:
MyLib::Func1();
As you can see, this can get quite ugly as more functions are added. At the very least, I'd like to separate it into different files!
I initially considered defining batches of functions in separate files in the MyLib
namespace and then using a MyLib.h
to consolidate all of them but I kept getting truckloads of linker errors - of course, I can take a closer look at this approach if it's recommended.
Any thoughts?
PS: Since most of these functions have different objectives it doesn't make sense to group them under a class from which we'd instantiate objects. I've used a class
here so I won't have to worry about the order in which I've defined the functions (there is interdependence among functions within MyLib
as well).
Linker Errors:
So the basic structure's like this: I have two classes (say A & B) which compile to static libraries and a master application which runs instances of these classes. These classes A & B use functions in MyLib
. When A & B are compiling I get the LNK4006
warning which states that symbols belonging to MyLib
have already been defined in an OBJ file within the project and it's ignoring it.
When it comes down to the application it becomes an LNK2005
error which states that it's already defined in the OBJ files of A & B.
UPDATE: Thank you Mike & Mathieu for the inline idea - it was the problem!
Except for one issue: I have some template functions which I've explicitly specialized and these are causing the already defined
error (LNK2005
):
template<class t> int Cvt(){}
template<> int Cvt<unsigned char>(){return 1;}
template<> int Cvt<char>(){return 2;}
template<> int Cvt<unsigned short>(){return 3;}
Any ideas?
Conlusion:
Solved the explicit specialization problem by defining the template functions in a separate file - thanks for the help!