I have the following header file containing a template class:
我有以下包含模板类的头文件:
#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
The .tpp file has all the implementations of the methods of the Vector template class. I also have a Vector.cpp file, which defines the factory functions, like this:
.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;
}
}
}
I get the errors: size_t is not a member of std, and ostream is not a member of std. If I delete everything in the .cpp file and don't use the factories, everything's fine. So what could be the problem?
我得到错误:size_t不是std的成员,ostream不是std的成员。如果我删除.cpp文件中的所有内容而不使用工厂,一切都很好。那可能是什么问题呢?
1 个解决方案
#1
3
Write
写
#ifndef VECTOR_H
#define VECTOR_H
#include <iosfwd>
#include <cstddef>
namespace lgl
{
//...
#1
3
Write
写
#ifndef VECTOR_H
#define VECTOR_H
#include <iosfwd>
#include <cstddef>
namespace lgl
{
//...