爲什麼當我調用它們時,我得到對這個類中的方法的未定義引用?我會被迫將實現包含在頭文件中,還是有另一種方法可以更好地實現這一點?對C++模板方法的函數未定義的引用
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() { }
};
你是否將函數定義在至少如果不在同一個頭文件中的地方? – ckv 2010-06-24 08:31:38
它們在相應的.cpp文件中。我在另一篇文章中看到,對於模板方法,如果編譯器不在頭文件中,編譯器就找不到這些定義。 – 2010-06-24 08:36:11
要改進問題中提供的信息:未定義的符號是什麼?模板化方法在哪裏定義?你鏈接了所有需要的文件嗎? – 2010-06-24 08:43:46