std :: unique_ptr <mesh> :: unique_ptr(__ gnu_cxx :: __ alloc_traits <std :: allocator <mesh>&gt; :: value_type&amp;)'

时间:2016-07-04 15:36:35

标签: c++ c++11 3d unique-ptr

我正在使用C ++ std11,我有以下代码

module dff_rstL (q,qn,clk,d, clearL);
input  clk,d, clearL ;
output q,qn;
reg q;
always @(posedge clk or negedge clearL)            //asynchronous reset
 begin
    if (~clearL) begin
     q <= 1'b0;
    end
 else    begin
        q <= d;
   end
end
assign  qn=~q;
endmodule

我的班级#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[]并且我创建一个{{1} }}

感谢。

PS。那些是我的班级:

Mesh

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

您将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 */) );

问题是您尝试使用myMesh初始化Mesh(非指向Mesh的指针)

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

std::unique_ptr<T>没有接收T对象的构造函数;例如,你可以在this page中看到它。

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

答案 2 :(得分:0)

类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;
}

class Model

#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