我正在尝试使用基于模板的方法在C ++中实现基本的2D矢量类。 我的班级看起来像
template <typename T>
class Vector2 {
public:
union {
struct {
T x,y;
};
struct {
T lon, lat;
};
};
Vector2():x(0), y(0) {}
Vector2(const T c):x(c), y(c) {}
Vector2(const Vector2<T> & v):x(v.x), y(v.y){}
Vector2(const T _x, const T _y):x(_x), y(_y) {}
};
现在我想添加一些像
这样的运算符inline template <typename T> Vector2<T> operator + (const Vector2<T>& a, const Vector2<T>& b){return Vector2<T>(a.x + b.x, a.y + b.y);}
对于开发我目前正在使用XCode,而Apple的LLVM编译器会编译所有内容。由于我需要在Linux系统上另外编译,我也想使用gcc。但是我的Linux系统(Fedora,gcc版本4.1.2)和我的mac(也是gcc版本4.1.2)编译都失败了,我收到了错误
错误:在'&lt;'之前预期的unqualified-id令牌
我的基于小模板的辅助函数发生了同样的错误
inline template<typename T> Vector2<T> vector2Lerp(const Vector2<T>& A, const Vector2<T>& B,
const Vector2<T>& C, const Vector2<T>& D, const double x, const double y)
{
// use two helper Points
Vector2<T> P(A + x * (B - A));
Vector2<T> Q(C + x * (D - C));
// interpolate between helper Points
return P + y * (Q - P);
}
所以我的问题是,如果有人可以帮助我解决这个问题。 谢谢你的帮助!
答案 0 :(得分:4)
您在错误的地方使用inline
关键字。您应该在使用之前引入模板参数:
template <typename T> inline Vector2<T> operator + (....);
请注意,默认情况下,功能模板为inline
,因此您可以省略它。