C++矢量错误 C2036:"整数 (*)[]":未知大小

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

本文关键字:未知 整数 错误 C2036 C++      更新时间:2023-10-16

>我收到 5 个编译错误

C2036: "整数 (*)[]" : 未知大小

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

#include <glglew.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;
    }

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

错误 C2036:"代理 *const":"矢量"类中的大小未知和定义向量类型时出现前向声明错误?

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

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

你的问题来自这一行:

unique_ptr<vector<int[]>> m_indices;

您应该改用 stl 容器,在这种情况下,它可以是向量的向量

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

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))
{ }