0

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;

};
BulBul
  • 1,159
  • 3
  • 24
  • 37
  • 1
    `gltf` is not copyable, it likely has at least one member, `asset`, that is not copyable. – François Andrieux Aug 01 '18 at 19:29
  • 1
    The error message is telling you that `gltf` does not define a copy-constructor. You should write a copy constructor (keeping in mind the [rule-of-three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)), or define a move-constructor (which brings you to the [rule-of-five](https://en.cppreference.com/w/cpp/language/rule_of_three)) and then `push_back(std::move(stagingModel));` – alter_igel Aug 01 '18 at 19:30
  • @FrançoisAndrieux but what makes it not copyable? is there a way around this issue? – BulBul Aug 01 '18 at 21:01

1 Answers1

5

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.

Xirema
  • 19,889
  • 4
  • 32
  • 68
  • previously i was able to use this class to load 3d models (without using a vector for the class), so i presume the non copyable member is a direct member of gltf (probably asset as someone mentioned in the comments) i will modify the code and give feedback here. – BulBul Aug 01 '18 at 21:11
  • i created an explicit copy constructor and it worked, thanks for your help – BulBul Aug 03 '18 at 18:51