3
代碼中存在錯誤,但我不知道如何解決。它說「太少的模板參數列表」。我不明白哪個是錯誤的。「太少的模板參數列表」錯誤
下面是代碼:
#if !defined(VECTOR_H_INCLUDED)
# define VECTOR_H_INCLUDED
namespace Vec
{
class Vector_base
{
public:
explicit Vector_base() {}
virtual ~Vector_base() {}
};
template<typename T, int DIM>
class Vector : public Vector_base
{
typedef Vector<T,DIM> ME;
public:
explicit Vector()
{
memset(&v, 0, sizeof(v));
}
explicit Vector(T v[DIM])
{
memcpy(this->v, v, sizeof(this->v));
}
explicit Vector(T,T,T);
virtual ~Vector()
{
}
// Operators
T &operator [](size_t n)
{
return v[n];
}
T operator [](size_t n) const
{
return v[n];
}
ME &operator += (const ME& o)
{
for (size_t i=0; i<DIM; ++i)
{
v[i] += o[i];
}
return *this;
}
ME &operator -= (const ME& o)
{
for (size_t i=0; i<DIM; ++i)
{
v[i] -= o[i];
}
return *this;
}
ME &operator *= (const ME& o)
{
for (size_t i=0; i<DIM; ++i)
{
v[i] *= o[i];
}
return *this;
}
ME &operator /= (const ME& o)
{
for (size_t i=0; i<DIM; ++i)
{
v[i] /= o[i];
}
return *this;
}
ME &operator *= (double scalar)
{
for (size_t i=0; i<DIM; ++i)
{
v[i] *= scalar;
}
return *this;
}
ME &operator /= (double scalar)
{
for (size_t i=0; i<DIM; ++i)
{
v[i] /= scalar;
}
return *this;
}
const ME operator-(const ME &o) const
{
ME result = *this;
result -= o;
return result;
}
const ME operator+(const ME &o) const
{
ME result = *this;
result += o;
return result;
}
const ME operator*(const ME &o) const
{
ME result = *this;
result *= o;
return result;
}
const ME operator*(double scalar) const
{
ME result = *this;
result *= scalar;
return result;
}
const ME operator/(const ME &o) const
{
ME result = *this;
result /= o;
return result;
}
const ME operator/(double scalar) const
{
ME result = *this;
result /= scalar;
return result;
}
// Misc stuff
ME &get()
{
return v;
}
double length()
{
double temp = 0.0f;
for (size_t i=0; i<DIM; ++i)
{
temp += v[i] * v[i];
}
return sqrtf(temp);
}
void normalize()
{
double len = length();
for (size_t i=0; i<DIM; ++i)
{
v[i] /= len;
}
}
double dot(const ME &v) const;
private:
T v[DIM];
};
typedef Vector<double,3> Vector3;
double Vector3::dot(const ME &o) const // ----- it gives me the error here ...
{
return v[0] * o[0] + v[1] * o[1] + v[2] * o[2];
}
Vector3::Vector(double x, double y, double z) // ----- ... and here
{
v[0] = x;
v[1] = y;
v[2] = z;
}
}
#endif // VECTOR_H_INCLUDED
我有什麼改變?
在此先感謝。對於我的英語感到抱歉,我在學校學習英語作爲輔助語言。
啊,謝謝。現在它工作了! :) – Ale