Why am I getting undefined references to the methods in this class when I call them? Will I be forced to include the implementation in the header file or is there another way to do this better?
class MathHelper
{
public:
/*!
Represents the ratio of the circumference of a circle to its diameter,
specified by the constant, p. This value is accurate to 5 decimal places.
*/
static const double pi = 3.14159;
template <typename T> static const T modulo(const T &numerator, const T &denominator);
static const double modulo(double numerator, double denominator);
static const float modulo(float numerator, float denominator);
template <typename T> static const T& clamp(const T &value, const T &min, const T &max);
template <typename T> static const T wrap(const T &value, const T &min, const T &max);
template <typename T> static bool isPowerOfTwo(T number);
template <typename T> static T nearestPowerOfTwo(T number);
static float aspectRatio(const QSize &size);
template <typename T> static float aspectRatio(T width, T height);
template <typename T> static T degreesToRadians(T degrees);
template <typename T> static T radiansToDegrees(T radians);
template <typename T> static T factorial(T n);
private:
MathHelper() { }
};
I think the explanation and answer to your question is this C++ faq lite answer and the next ones
Basically, as templates are patterns to instanciate, any code unit needing it must know how to instanciate it. Therefore, the simpliest way is to define your templates in header files (like boost does). The C++ faq lite give another way to do that. In my humble opinion, I think it is cumbersome…
my2c