- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下包含模板类的头文件:
#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/
error C2664: 'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)' : cannot convert pa
size_t有什么区别和 std::size_t在声明它们的位置,何时应该使用它们以及任何其他差异化功能方面? 最佳答案 C的size_t和 C++ 的 std::size_t都是一样的。 在 C 中
刚读完: Does "std::size_t" make sense in C++? 我意识到使用 ::size_t不符合标准(虽然我的编译器支持)当你#include .我想遵守标准,但我不想在前
有什么理由不假设 SIZE_T 是 Microsoft 的 Visual C/C++ 编译器上 size_t 的类型定义? Windows intsafe.h 函数确实包括从一个函数到另一个函数的安全
size_t 和 std::size_t 在声明的位置、应在何时使用以及任何其他区别特性方面有何区别? 最佳答案 C 的 size_t和 C++ 的 st
在某些 Windows API 调用中,我得到:cannot convert argument 6 from 'size_t *' to 'SIZE_T *' . This answer告诉我SIZE
将迭代器条件右操作数从 size_t 转换为 int 更好,还是迭代可能超过 int 的最大值?答案实现具体吗? int a; for (size_t i = 0; i (i)) (有关 numeri
我尝试了三个连续的行(每个单独一个),但是没有一个起作用。 为什么?? int main() { size_t j{8}; char arr[static_cast(j)]={'t'}
我的 C++ 知识一塌糊涂。我有 Apple 提供的代码,他们像往常一样提供了不完整的解决方案。 在此代码中,他们提供了两个空的方法 header : - (NSString *)encodeBase
这是证据: inline constexpr std::size_t prev(std::size_t i) { --i; return i; } int main() { s
我对 size_t 的某些行为感到困惑我注意到: size_t zero = 0x1 << 32; size_t big = 0x1 << 31; size_t not_as_big = 0x1 <<
size_t 在哪里什么时候我什么都没有? 总是假设 size_t 是否合理? == std::size_t ? 什么时候应该使用 size_type在 std容器(string::size_type
我在为 32 位编译时遇到此错误。相同的文件在 64 位 Windows 上编译没有错误 1>c:\project\test.cpp(1317) : error C2664: 'StringCbCop
我希望只有一个模板函数。所以我想到了…… template > || std::is_same_v > > > std::ostream& op
我正在使用 Visual Studio 2017 社区版。它允许我在没有适当包含的情况下同时使用 size_t 和 std::size_t。它似乎适用于大多数 std 库。我认为这是因为图书馆本身的一
我可能要疯了,但我认为我从未在 C++ 中看到过这种情况(尽管我的引用代码是在 C 中)。为什么这里代码的返回值上有一个static,有什么影响?我认为我从未见过类范围之外的静态函数(但显然 C 没有
它没有在 std::string 中明确列出 constructor doc ,(编辑: 这里的人说我应该引用实际的 cppreference 而不是 cplusplus.com)但显然它有效。这意味
将 -1 分配给 size_t 类型的变量 t 并检查它与 -1 和 4294967295(FFFFFFFF,2 对-1 的补充;我的系统是64 位;值可能因系统而异),那么在这两种情况下,它都返回
我正在实现循环数组数据结构,其代码如下所示: struct CircularArrayException : public std::exception { std::string msg;
这个问题在这里已经有了答案: how to pass 2 dimensional array if both dimensions are unknown at compile time (5 个答
我是一名优秀的程序员,十分优秀!