简体   繁体   中英

vector push_back() gives the compiler error C2280

I am trying to append a custom class object to a vector of the same type, however when i try to compile my code, the compiler gives the following error

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

my function takes a string (file name) as an argument and loads that file, then parses the file and populates it's variables

the function is as follows:-

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
}

my gltf class decleration is as follows:-

#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;

};

The error you're getting is the consequence of gltf being a non-copyable object, because its copy constructor was implicitly deleted. Calling push_back on a vector requires that the pushed object either be copyable or movable, and in the latter case, you need to expressly pass an r-value, which you are not doing.

You haven't explicitly deleted the copy constructor in gltf , so what I have to presume is that one or more of the objects stored in gltf , or possibly one of the objects stored in the (many) vectors inside gltf , is itself non-copyable, which caused your class to implicitly delete its own copy constructor. Do a code audit to find out which object isn't copyable, and either remove it or write your own copy constructor (and probably also move constructor) for this class, or retrofit that object to be properly copyable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM