C++ vector 错误 C2036 : 'int (*)[]' : unknown size

标签 c++ templates c++11 vector

我遇到了 5 个编译错误

C2036: 'int (*)[]' : unknown size

全部来自 vector 类中的不同位置。

#include <gl\glew.h>
#include "Vector2.h"
#include "Vector3.h"
#include "WFObjModel.h"
#include <vector>
#include <memory>

using namespace math;
using std::vector;
using std::string;
using io::WFObjModel;
using std::unique_ptr;

class Mesh
{
private:
    GLuint textureID;
    unique_ptr<vector<Vector3<float>>> m_vertices;
    unique_ptr<vector<Vector3<float>>> m_normals;
    unique_ptr<vector<Vector2<float>>> m_textureCoordinates;
    unique_ptr<vector<int[]>> m_indices;
public:
    Mesh(unique_ptr<vector<Vector3<float>>> vertices,
        unique_ptr<vector<Vector3<float>>> normals,
        unique_ptr<vector<Vector2<float>>> textureCoordinates,
        unique_ptr<vector<int[]>> indices);

    Mesh(Mesh&& other){
        m_vertices = std::move(other.m_vertices);
        m_normals = std::move(other.m_normals);
        m_textureCoordinates = std::move(other.m_textureCoordinates);
        m_indices = std::move(other.m_indices);
    }

    Mesh& operator=(Mesh&& other)
    {
        m_vertices = std::move(other.m_vertices);
        m_normals = std::move(other.m_normals);
        m_textureCoordinates = std::move(other.m_textureCoordinates);
        m_indices = std::move(other.m_indices);
        return *this;
    }

我查看了围绕此问题的其他答案,但公认的解决方案似乎不起作用/不适用。

error C2036: 'Agent *const ' : unknown size in 'vector' classForward declaration error when defining a vector type?

似乎暗示错误是由于编译器没有存储在 vector 中的类型的完整定义。我有一个包含存储在 vector 中的仅 header 模板类,但我猜这与模板类的编译方式有关?

我似乎无法为模板添加前向声明 class Vector3<float>没有编译器认为我正在尝试专门化模板。

最佳答案

您的问题来自这一行:

unique_ptr<vector<int[]>> m_indices;

您应该改用 STL 容器,在这种情况下它可以是 vector 的 vector

此外,为什么在这种情况下需要 unique_ptr? vector 支持移动语义,你可以有一个

vector<vector<int>> m_indices;

关于移动构造函数的额外提示,通常的做法是像这样实现它们:

Mesh(Mesh&& other)
: m_vertices(std::move(other.m_vertices)
, m_normals(std::move(other.m_normals))
, textureCoordinates(std::move(other.m_textureCoordinates))
, indices(std::move(other.m_indices))
{ }

关于C++ vector 错误 C2036 : 'int (*)[]' : unknown size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30911461/

相关文章:

c++ - 模板成员函数特化

templates - 相当于 ASP Classic 中的母版页

linux - CMake:未使用 CXX_STANDARD 设置 C++11

C++11 - 将非静态数据成员声明为 'auto'

c++ - 创建一个 TH2,其中包含给定 bin 中变量的标准差

c++ - 删除字符矩阵

c++ - 在使用声明引入的折叠表达式中使用运算符是否合法?

c++ - 还需要最终确定的 CRTP 中间类

c++ - C++中的24位到32位转换

c++ - C++中的简单位图图形?