gpt4 book ai didi

c++ - 误导性编译器错误消息 : size_t is not a member of std?

转载 作者:太空狗 更新时间:2023-10-29 21:17:58 26 4
gpt4 key购买 nike

我有以下包含模板类的头文件:

#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
{
//...

关于c++ - 误导性编译器错误消息 : size_t is not a member of std?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31212452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com