テンプレートベースのアプローチを使用して、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 Compiler がすべてをコンパイルします。Linux システムで追加でコンパイルする必要があるため、gcc も使用したいと考えています。しかし、Linux システム (Fedora、gcc バージョン 4.1.2) と Mac (gcc バージョン 4.1.2) の両方でコンパイルが失敗し、エラーが発生します。
エラー: '<' トークンの前に 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);
}
だから私の質問は、誰かがこの問題を解決するのを手伝ってくれるかどうかです。ご協力ありがとうございました!