std::unique_ptr<Mesh>::unique_ptr(__gnu_cxx::__alloc_traits<std:::分配器<Mesh> >::val

std::unique_ptr<Mesh>::unique_ptr(__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type&)’

本文关键字:lt gt std Mesh ptr unique 分配器 val traits cxx gnu      更新时间:2023-10-16

我使用c++ std11,我有以下代码

#include "RenderEngine.h"
#include "Cam.h"
#include "Vec3d.h"
#include "Mesh.h"
#include "Model.h"
#include <vector>
#include <memory>
struct Option
{
  int imageWidth, imageHeight;
};
int main()
{
Cam* cam = new Cam(Vec3d(0.0, 0.0, 250.0), Vec3d(0.0, 0.0, -1.0), 90, 1);
//Cam* cam = new Cam();
Option option;
option.imageWidth = 1920;
option.imageHeight = 1080;
//option.imageWidth = 1280;
//option.imageHeight = 900;
Model myModel;
myModel.loadModel("/models/arm.obj");
std::vector<std::unique_ptr<Mesh>> objects;
std::unique_ptr<Mesh> myMesh( myModel.meshes[0] );
objects.push_back(std::move(myMesh));
RenderEngine* render = new RenderEngine(*cam, option.imageWidth, option.imageHeight);
render->getImage(objects);
delete cam;
delete render;
return 0;
}

,我的类Model包含一个std::vector<Mesh> meshes;

当我编译这段代码,我是以下错误:

    andreaolivieri@andreaolivieri-Aspire-V3-571:~/Documenti/C++/Progetti_POG/002_Beta$ make
g++ -Wall -c -g -std=c++11  testRender.cpp -o obj/testRender.o -Llib
testRender.cpp: In function ‘int main()’:
testRender.cpp:27:50: error: no matching function for call to ‘std::unique_ptr<Mesh>::unique_ptr(__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type&)’
  std::unique_ptr<Mesh> myMesh( myModel.meshes[0] );
                                                  ^
testRender.cpp:27:50: note: candidates are:
In file included from /usr/include/c++/4.8/memory:81:0,
                 from RenderEngine.h:13,
                 from testRender.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:176:2: note: template<class _Up, class> std::unique_ptr<_Tp, _Dp>::unique_ptr(std::auto_ptr<_Up>&&)
  unique_ptr(auto_ptr<_Up>&& __u) noexcept;
  ^
/usr/include/c++/4.8/bits/unique_ptr.h:176:2: note:   template argument deduction/substitution failed:
testRender.cpp:27:50: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ is not derived from ‘std::auto_ptr<_Up>’
  std::unique_ptr<Mesh> myMesh( myModel.meshes[0] );
                                                  ^
In file included from /usr/include/c++/4.8/memory:81:0,
                 from RenderEngine.h:13,
                 from testRender.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:169:2: note: template<class _Up, class _Ep, class> std::unique_ptr<_Tp, _Dp>::unique_ptr(std::unique_ptr<_Up, _Ep>&&)
  unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
  ^
/usr/include/c++/4.8/bits/unique_ptr.h:169:2: note:   template argument deduction/substitution failed:
testRender.cpp:27:50: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ is not derived from ‘std::unique_ptr<_Tp, _Dp>’
  std::unique_ptr<Mesh> myMesh( myModel.meshes[0] );
                                                  ^
In file included from /usr/include/c++/4.8/memory:81:0,
                 from RenderEngine.h:13,
                 from testRender.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:160:7: note: std::unique_ptr<_Tp, _Dp>::unique_ptr(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = Mesh; _Dp = std::default_delete<Mesh>]
       unique_ptr(unique_ptr&& __u) noexcept
       ^
/usr/include/c++/4.8/bits/unique_ptr.h:160:7: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ to ‘std::unique_ptr<Mesh>&&’
/usr/include/c++/4.8/bits/unique_ptr.h:157:17: note: constexpr std::unique_ptr<_Tp, _Dp>::unique_ptr(std::nullptr_t) [with _Tp = Mesh; _Dp = std::default_delete<Mesh>; std::nullptr_t = std::nullptr_t]
       constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
                 ^
/usr/include/c++/4.8/bits/unique_ptr.h:157:17: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ to ‘std::nullptr_t’
/usr/include/c++/4.8/bits/unique_ptr.h:151:7: note: std::unique_ptr<_Tp, _Dp>::unique_ptr(std::unique_ptr<_Tp, _Dp>::pointer, typename std::remove_reference<_To>::type&&) [with _Tp = Mesh; _Dp = std::default_delete<Mesh>; std::unique_ptr<_Tp, _Dp>::pointer = Mesh*; typename std::remove_reference<_To>::type = std::default_delete<Mesh>]
       unique_ptr(pointer __p,
       ^
/usr/include/c++/4.8/bits/unique_ptr.h:151:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/4.8/bits/unique_ptr.h:146:7: note: std::unique_ptr<_Tp, _Dp>::unique_ptr(std::unique_ptr<_Tp, _Dp>::pointer, typename std::conditional<std::is_reference<_Dp>::value, _Dp, const _Dp&>::type) [with _Tp = Mesh; _Dp = std::default_delete<Mesh>; std::unique_ptr<_Tp, _Dp>::pointer = Mesh*; typename std::conditional<std::is_reference<_Dp>::value, _Dp, const _Dp&>::type = const std::default_delete<Mesh>&]
       unique_ptr(pointer __p,
       ^
/usr/include/c++/4.8/bits/unique_ptr.h:146:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/4.8/bits/unique_ptr.h:141:7: note: std::unique_ptr<_Tp, _Dp>::unique_ptr(std::unique_ptr<_Tp, _Dp>::pointer) [with _Tp = Mesh; _Dp = std::default_delete<Mesh>; std::unique_ptr<_Tp, _Dp>::pointer = Mesh*]
       unique_ptr(pointer __p) noexcept
       ^
/usr/include/c++/4.8/bits/unique_ptr.h:141:7: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ to ‘std::unique_ptr<Mesh>::pointer {aka Mesh*}’
/usr/include/c++/4.8/bits/unique_ptr.h:135:17: note: constexpr std::unique_ptr<_Tp, _Dp>::unique_ptr() [with _Tp = Mesh; _Dp = std::default_delete<Mesh>]
       constexpr unique_ptr() noexcept
                 ^
/usr/include/c++/4.8/bits/unique_ptr.h:135:17: note:   candidate expects 0 arguments, 1 provided
make: *** [obj/testRender.o] Errore 1

请我不明白错误在哪里,我有一个Mesh的矢量,并使用operator[]给我一个Mesh,并与此创建一个unique_ptr<Mesh>

谢谢。

p。这些是我的类:

#ifndef _MODEL_H
#define _MODEL_H
#include "Mesh.h"
#include <vector>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
class Model
{
    public:
        std::vector<Mesh> meshes;
        std::string directory;
        void loadModel(std::string path);
        void processNode(aiNode* node, const aiScene* scene);
        Mesh processMesh(aiMesh* mesh, const aiScene* scene);
};
#endif

#ifndef _MESH_H
#define _MESH_H
#include "Vec3d.h"
#include "Ray.h"
#include <vector>
class Mesh
{
    protected:
        unsigned int numVertices, numFaces;
        std::vector<unsigned int> vertexIndex;
        std::vector<Vec3d> verts;
        std::vector<Vec3d> vertexNormals;
        Vec3d cl;
    public:
        Mesh(unsigned int &_numVertices, unsigned int &_numFaces, std::vector<unsigned int> &_vertexIndex, std::vector<Vec3d> &_verts, std::vector<Vec3d> &_vertexNormals);
        bool intersect(Ray &r, double &t, double &u, double &v);
        inline const Vec3d getCL(){ return cl; }
};
#endif

使用函数std::make_unique代替构造函数(第27行):

std::unique_ptr<Mesh> myMesh = std::make_unique (myModel.meshes[0]);

包含<memory>以使用std::make_unique !

您将myMesh声明为std::unique_ptr<Mesh>,因此您应该使用默认值(nullptr)初始化它

 std::unique_ptr<Mesh> myMesh; // initialized to nullptr

或与指针指向已分配的 Mesh;就像

 std::unique_ptr<Mesh> myMesh( new Mesh(/* arguments to constructor */) );

问题是你试图用Mesh(非指向Mesh的指针)初始化myMesh

 std::unique_ptr<Mesh> myMesh( myModel.meshes[0] );
                            // myModel.meshes[0] is a Mesh

没有std::unique_ptr<T>的构造函数可以接收T对象;你可以在这一页看到它的例子。

注意:避免将指向myModel.meshes[0]的指针传递给myMesh构造函数,因为它不是一个已分配的Mesh;这是错误的,也是危险的。

class Mesh

#include "Mesh.h"
#include "Ray.h"
#include "Triangle.h"
#include <vector>
Mesh::Mesh(unsigned int &_numVertices, unsigned int &_numFaces, std::vector<unsigned int> &_vertexIndex, std::vector<Vec3d> &_verts, std::vector<Vec3d> &_vertexNormals)
{
    numVertices = _numVertices;
    numFaces = _numFaces;
    vertexIndex = std::move(_vertexIndex);
    verts = std::move(_verts);
    vertexNormals = std::move(_vertexNormals);
    cl = Vec3d(0.5, 0.5, 0.5);
}

Mesh::Mesh(const Mesh& m) // Copy constructor
{
    numVertices = m.numVertices;
    numFaces = m.numFaces;
    vertexIndex = m.vertexIndex;
    verts = m.verts;
    vertexNormals = m.vertexNormals;
    cl = m.cl;
}
Mesh& Mesh::operator=(const Mesh& m) // Copy assignment
{
    numVertices = m.numVertices;
    numFaces = m.numFaces;
    vertexIndex = m.vertexIndex;
    verts = m.verts;
    vertexNormals = m.vertexNormals;
    cl = m.cl;
    return *this;
}
Mesh::Mesh(Mesh&& m) // Move constructor
{
    numVertices = m.numVertices;
    numFaces = m.numFaces;
    vertexIndex = std::move(m.vertexIndex);
    verts = std::move(m.verts);
    vertexNormals = std::move(m.vertexNormals);
    cl = m.cl;
    m.numVertices = 0;
    m.numFaces = 0;
    m.cl = Vec3d();
}
Mesh& Mesh::operator=(Mesh&& m) // Move assignment
{
    numVertices = m.numVertices;
    numFaces = m.numFaces;
    vertexIndex = std::move(m.vertexIndex);
    verts = std::move(m.verts);
    vertexNormals = std::move(m.vertexNormals);
    cl = m.cl;
    m.numVertices = 0;
    m.numFaces = 0;
    m.cl = Vec3d();
    return *this;
}

类模型
#include "Model.h"
#include "Mesh.h"
#include "Vec3d.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
Model::Model(std::string path)
{
    std::cout << "Model constructor " << std::endl;
    this->loadModel(path);
}
void Model::loadModel(std::string &path)
{
    std::cout << "Load Model " << std::endl;
    // Read file via ASSIMP
    Assimp::Importer importer;
    const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_SortByPType | aiProcess_JoinIdenticalVertices);
    // Check for errors 
    if(!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
    {
        std::cout << "ERROR::ASSIMP:: " << importer.GetErrorString() << std::endl;
        return;
    }
    // Retrieve the directory path of the filepath
    this->directory = path.substr(0, path.find_last_of('/'));
    // Process ASSIMP's root node recursively
    this->processNode(scene->mRootNode, scene);
}
// Processes a node in a recursive fashion. Processes each individual mesh located at the node and repeats this process on its children nodes (if any).
void Model::processNode(aiNode* node, const aiScene* scene)
{
    std::cout << "Process node " << std::endl;
    // Process each mesh located at the current node
    for(unsigned int i = 0; i < node->mNumMeshes; i++)
    {
        // The node object only contains indices to index the actual objects in the scene. 
        // The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
        int meshIndex = node->mMeshes[i];
        aiMesh* mesh = scene->mMeshes[ meshIndex ]; 
        this->meshes.push_back(this->processMesh(mesh, scene));         
    }
    // After we've processed all of the meshes (if any) we then recursively process each of the children nodes
    for(unsigned int i = 0; i < node->mNumChildren; i++)
    {
        this->processNode(node->mChildren[i], scene);
    }
}
Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene)
{
    std::cout << "Process Mesh " << std::endl;
    // Data to fill
    std::vector<Vec3d> verts;
    std::vector<Vec3d> vertexNormals;
    std::vector<unsigned int> vertexIndex;
    // Walk through each of the mesh's vertices
    for(unsigned int i = 0; i < mesh->mNumVertices; i++)
    {
        std::cout << "Process vertex ";
        Vec3d position, normal;
        // Positions
        if ( mesh->mVertices )
        {
            std::cout << " position ";
            position.setX((double)mesh->mVertices[i].x);
            std::cout << " position.x " << position.getX();
            position.setY((double)mesh->mVertices[i].y);
            std::cout << " position.y " << position.getY();
            position.setZ((double)mesh->mVertices[i].z);
            std::cout << " position.z " << position.getZ();
        }
        // Normals
        if ( mesh->mNormals )
        {
            std::cout << " normal ";
            normal.setX((double)mesh->mNormals[i].x);
            normal.setY((double)mesh->mNormals[i].y);
            normal.setZ((double)mesh->mNormals[i].z);
        }
        std::cout << std::endl;
        verts.push_back(position);
        vertexNormals.push_back(normal);
    }
    // Now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
    for(unsigned int i = 0; i < mesh->mNumFaces; i++)
    {
        std::cout << "Process face" << std::endl;
        aiFace face = mesh->mFaces[i];
        // Retrieve all indices of the face and store them in the indices vector
        for(unsigned int j = 0; j < face.mNumIndices; j++)
        {
            vertexIndex.push_back(face.mIndices[j]);
            std::cout << face.mIndices[j] << std::endl;
        }
        std::cout << "" << std::endl;
    }
    // Return a mesh object created from the extracted mesh data
    std::cout << "Return mesh" << std::endl;
    return Mesh(mesh->mNumVertices, mesh->mNumFaces, vertexIndex, verts, vertexNormals);
}

主类

 #include "RenderEngine.h"
#include "Cam.h"
#include "Vec3d.h"
#include "Mesh.h"
#include "Model.h"
#include <vector>
#include <memory>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
struct Option
{
    int imageWidth, imageHeight;
};
int main()
{
    Cam* cam = new Cam(Vec3d(0.0, 0.0, 10000.0), Vec3d(0.0, 0.0, -1.0), 90, 1);
    //Cam* cam = new Cam();
    Option option;
    option.imageWidth = 512;
    option.imageHeight = 512;
    //option.imageWidth = 1920;
    //option.imageHeight = 1080;
    //option.imageWidth = 1280;
    //option.imageHeight = 900;
    std::string path = "model/cube.obj";
    Model myModel(path);
    //myModel.loadModel();
    std::vector<std::unique_ptr<Mesh>> objects;
    //std::unique_ptr<Mesh> myMesh = std::make_unique (myModel.meshes[0]); //first try
    std::unique_ptr<Mesh> myMesh; // initialized to nullptr
    myMesh = (myModel.meshes[0]);   //second try
    //myMesh = new Mesh(myModel.meshes[0].numVertices, myModel.meshes[0].numFaces, myModel.meshes[0].vertexIndex, myModel.meshes[0].verts, myModel.meshes[0].vertexNormals);
    //std::unique_ptr<Mesh> myMesh( new Mesh(myModel.meshes[0].numVertices, myModel.meshes[0].numFaces, myModel.meshes[0].vertexIndex, myModel.meshes[0].verts, myModel.meshes[0].vertexNormals) );
    objects.push_back(std::move(myMesh));
    RenderEngine* render = new RenderEngine(*cam, option.imageWidth, option.imageHeight);
    render->getImage(objects);
    delete cam;
    delete render;
    return 0;
}
        render->getImage(objects);
        delete cam;
        delete render;
        return 0;
    }

出现以下错误:

testRender.cpp: In function ‘int main()’:
testRender.cpp:39:9: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<Mesh>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’)
  myMesh = (myModel.meshes[0]);   //second try
         ^
testRender.cpp:39:9: note: candidates are:
In file included from /usr/include/c++/4.8/memory:81:0,
                 from RenderEngine.h:13,
                 from testRender.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:190:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = Mesh; _Dp = std::default_delete<Mesh>]
       operator=(unique_ptr&& __u) noexcept
       ^
/usr/include/c++/4.8/bits/unique_ptr.h:190:7: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ to ‘std::unique_ptr<Mesh>&&’
/usr/include/c++/4.8/bits/unique_ptr.h:203:2: note: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> > >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = Mesh; _Dp = std::default_delete<Mesh>]
  operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  ^
/usr/include/c++/4.8/bits/unique_ptr.h:203:2: note:   template argument deduction/substitution failed:
testRender.cpp:39:9: note:   ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ is not derived from ‘std::unique_ptr<_Tp, _Dp>’
  myMesh = (myModel.meshes[0]);   //second try
         ^
In file included from /usr/include/c++/4.8/memory:81:0,
                 from RenderEngine.h:13,
                 from testRender.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h:211:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = Mesh; _Dp = std::default_delete<Mesh>; std::nullptr_t = std::nullptr_t]
       operator=(nullptr_t) noexcept
       ^
/usr/include/c++/4.8/bits/unique_ptr.h:211:7: note:   no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<Mesh> >::value_type {aka Mesh}’ to ‘std::nullptr_t’
make: *** [obj/testRender.o] Errore 1