1
我有一個包含一個模板類下面的頭文件:誤導性的編譯器錯誤信息:size_t不是std的成員?
#ifndef VECTOR_H
#define VECTOR_H
namespace lgl
{
namespace maths
{
template<class T, std::size_t SIZE>
class Vector
{
public:
Vector();
Vector(T defaultValue);
Vector(const Vector<T, SIZE>& other);
Vector<T, SIZE>& operator=(const Vector<T, SIZE>& other);
~Vector();
//accessors
const std::size_t size() const;
const T& operator[](std::size_t i) const;
T& operator[](std::size_t i);
//vector operations
Vector<T, SIZE> operator+(const Vector<T, SIZE>& other) const;
Vector<T, SIZE> operator-(const Vector<T, SIZE>& other) const;
Vector<T, SIZE> operator*(const T& scalar) const ;
Vector<T, SIZE> operator/(const T& scalar) const ;
T operator*(const Vector<T, SIZE>& other) const;
void operator+=(const Vector<T, SIZE>& other);
void operator-=(const Vector<T, SIZE>& other);
void operator*=(const T& scalar);
void operator/=(const T& scalar);
bool operator==(const Vector<T, SIZE>& other) const;
bool operator!=(const Vector<T, SIZE>& other) const;
private:
T m_elements[SIZE];
};
template<class T, std::size_t SIZE>
std::ostream& operator<<(std::ostream& os, const Vector<T, SIZE>& vec);
template<class T>
Vector<T, 3> cross(const Vector<T, 3>& a, const Vector<T, 3>& b);
#include "Vector.tpp"
//typedefs
typedef Vector<float, 2> vec2f;
typedef Vector<float, 3> vec3f;
typedef Vector<float, 4> vec4f;
typedef Vector<double, 2> vec2d;
typedef Vector<double, 3> vec3d;
typedef Vector<double, 4> vec4d;
typedef Vector<int, 2> vec2i;
typedef Vector<int, 3> vec3i;
typedef Vector<int, 4> vec4i;
//factories
vec2f getVec2f(float x, float y);
vec3f getVec3f(float x, float y, float z);
vec4f getVec4f(float x, float y, float z, float h);
}
}
#endif
的.tpp文件具有的矢量模板類的方法的所有實施方式。我也有一個Vector.cpp文件,它定義了工廠的功能,如:
#include "Vector.h"
namespace lgl
{
namespace maths
{
//factories
vec2f getVec2f(float x, float y)
{
vec2f result;
result[0] = x;
result[1] = y;
return result;
}
vec3f getVec3f(float x, float y, float z)
{
vec3f result;
result[0] = x;
result[1] = y;
result[2] = z;
return result;
}
vec4f getVec4f(float x, float y, float z, float h)
{
vec4f result;
result[0] = x;
result[1] = y;
result[2] = z;
result[3] = h;
return result;
}
}
}
我得到的錯誤:爲size_t是不是性病中的一員,而ostream的是不是性病的成員。 如果我刪除.cpp文件中的所有內容,並且不使用工廠,一切都很好。那麼可能是什麼問題?
是否包含適當的標題,如''? –
發佈完整的代碼。 '#include'在這裏很重要。順便說一句,'ostream'在''和'size_t'在''中。 –
lisyarus
作爲一個方面的說明,你可以看看std :: array在C++ 11,因爲你幾乎重新發明它... –