我有以下包含模板类的头文件:
#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模板类的方法的所有实现。我还有一个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不是std的成员,而ostream不是std的成员。
如果删除.cpp文件中的所有内容并且不使用工厂,则一切正常。那么可能是什么问题呢?
最佳答案
写
#ifndef VECTOR_H
#define VECTOR_H
#include <iosfwd>
#include <cstddef>
namespace lgl
{
//...