【问题标题】:vector push_back() gives the compiler error C2280vector push_back() 给出编译器错误 C2280
【发布时间】:2018-08-01 19:26:58
【问题描述】:

我正在尝试将自定义类对象附加到相同类型的向量中,但是当我尝试编译我的代码时,编译器会给出以下错误

gltf::gltf(const gltf &): attempting to reference a deleted function

我的函数将字符串(文件名)作为参数并加载该文件,然后解析该文件并填充它的变量

功能如下:-

void enigma::loadModel(std::string file)
{       

    gltf stagingModel;
    stagingModel.loadAsset(file);   // populates my object with data
    model.push_back(stagingModel);  // appends the populated object to a vector array (this line generates the error)

    ....   // the rest of the code
}

我的 gltf 类声明如下:-

#pragma once
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include "meshClass.h"
#include "bufferClass.h"
#include <gtx/quaternion.hpp>
#include <gtx/transform.hpp>

#include"stagingNodeClass.h"
#include"stagingMeshClass.h"
#include"stagingAccessorClass.h"
#include"stagingBufferViewClass.h"
#include"stagingImageClass.h"
#include"stagingSamplerClass.h"
#include"stagingTextureClass.h"
#include"stagingMaterialClass.h"
#include"stagingSkinClass.h"

class gltf
{
public:
    gltf();
    ~gltf();

    std::vector<meshClass> mesh;
    std::vector<char> bin;
    glm::mat4 camera;
    glm::mat4 scale;
    void loadAsset(std::string);

private:
    std::vector<stagingNodeClass> stagingNode;
    std::vector<stagingMeshClass> stagingMesh;
    std::vector<stagingAccessorClass> stagingAccessor;
    std::vector<stagingBufferViewClass>  stagingBufferView;
    std::vector<stagingImageClass> stagingImage;
    stagingSamplerClass stagingSampler;
    std::vector<stagingTextureClass> stagingTexture;
    std::vector<stagingMaterialClass> stagingMaterial;
    stagingSkinClass stagingSkins;
    bufferClass stagingBuffer;
    bool isCamera = false;

    bool node(std::string, std::string);
    int getValue(std::string);
    int getCamIndex(std::string);
    glm::mat4 getMatrix(std::string);
    glm::vec3 getVec3();
    glm::vec4 getVec4();
    float getFloatValue(std::string);
    void initScale();

    std::vector<int> getIntArray();
    std::vector<float> getFloatArray();

    std::string getName(std::string);

    std::fstream  asset;
    std::string line;

};

【问题讨论】:

  • gltf 不可复制,它可能至少有一个成员 asset,这是不可复制的。
  • 错误信息告诉您gltf 没有定义复制构造函数。您应该编写一个复制构造函数(记住rule-of-three),或定义一个移动构造函数(将您带到rule-of-five)然后push_back(std::move(stagingModel));
  • @FrançoisAndrieux 但是是什么让它不可复制?有没有办法解决这个问题?

标签: c++ c++11 vector c++14


【解决方案1】:

您得到的错误是gltf 是不可复制对象的结果,因为它的复制构造函数被隐式删除。在向量上调用 push_back 要求推送的对象是可复制的或可移动的,在后一种情况下,您需要明确传递一个 r 值,而您没有这样做。

您还没有明确删除gltf 中的复制构造函数,所以我必须假设存储在gltf 中的一个或多个对象,或者可能存储在(许多)向量中的对象之一在gltf 内部,它本身是不可复制的,这会导致您的类隐式删除它自己的复制构造函数。进行代码审核以找出哪个对象不可复制,然后将其删除或为此类编写自己的复制构造函数(并且可能还会移动构造函数),或者将该对象改造为可正确复制。

【讨论】:

  • 以前我能够使用这个类来加载 3d 模型(不使用类的向量),所以我认为不可复制的成员是 gltf 的直接成员(可能是资产,正如有人在cmets)我会在这里修改代码并给出反馈。
  • 我创建了一个显式的复制构造函数,它可以工作,感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2015-01-04
  • 2013-03-21
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多