代码中某处出现错误,但我不知道如何解决。它说“模板参数列表太少”。我不明白哪个是错误。
以下是代码:
#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
我需要改变什么?
提前致谢。对不起我的英语,我在学校学习英语作为第二语言。
答案 0 :(得分:5)
您应该在此处使用template<>
进行模板特化。
template<> double Vector3::dot(const ME &o) const
和
template<> Vector3::Vector(double x, double y, double z)