我试图将自定义类对象附加到相同类型的 vector ,但是当我尝试编译我的代码时,编译器给出了以下错误
gltf::gltf(const gltf &): 试图引用已删除的函数
我的函数接受一个字符串(文件名)作为参数并加载该文件,然后解析该文件并填充它的变量
函数如下:-
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
是一个不可复制对象的结果,因为它的复制构造函数被隐式删除了。在 vector 上调用 push_back
要求推送的对象是可复制的或可移动的,在后一种情况下,您需要明确传递一个 r 值,而您没有这样做。
您还没有明确删除 gltf
中的复制构造函数,所以我必须假设 gltf
中存储的一个或多个对象,或者可能是一个gltf
中存储在(许多) vector 中的对象本身是不可复制的,这导致您的类隐式删除其自己的复制构造函数。执行代码审核以找出哪个对象不可复制,然后删除它或为此类编写自己的复制构造函数(可能还有移动构造函数),或者改造该对象以使其可以正确复制。
关于c++ - vector push_back() 给出编译器错误 C2280,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51640949/