2017-08-02 12 views
2

私はちょうど stl::make_uniquemake_uniqueエラーが

は、現代のC++

に古いコードを変更する必要がスマートポインタを学ぶために始めている、私は(サンプルのコードの下の行をコンパイルするとき、私は次のエラーを取得していますそれは上記のサイトで.Lookedを働いているはずですし、可能な実装は

だったように、元のコード)

#include <memory> 
#include <iostream> 

using namespace std; 
struct Student 
{ 
    int id; 
    float Score; 
}; 
auto main()->int 
{ 
    auto Student1 = make_unique<Student>(1, 5.5); 
    //auto Student1 = make_unique<int>(1); //Works perfectly 
    //auto Student2 = unique_ptr<Student>{ new Student{1,22.5} }; //Works 
    cout << "working"; 
    return 0; 
} 

1>------ Build started: Project: ConsoleApplication4, Configuration: Debug Win32 ------ 
1>Source.cpp 
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\memory(2054): error C2661: 'Student::Student': no overloaded function takes 2 arguments 
1>c:\users\hsingh\documents\visual studio 2017\projects\consoleapplication4\consoleapplication4\source.cpp(12): note: see reference to function template instantiation 'std::unique_ptr<Student,std::default_delete<_Ty>> std::make_unique<Student,int,double>(int &&,double &&)' being compiled 
1>  with 
1>  [ 
1>   _Ty=Student 
1>  ] 
1>Done building project "ConsoleApplication4.vcxproj" -- FAILED. 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

私はmake_unique実装に見てみましたが見えます

// note: this implementation does not disable this overload for array types 
template<typename T, typename... Args> 
std::unique_ptr<T> make_unique(Args&&... args) 
{ 
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); 
} 

ので、私の質問は(私が持っているもの、今の周りの仕事は直接unique_ptrをを使用することです)

それは私が行うことができますどのような変更make_unique

  • を使用して操作できるようにする方法

    1. ですSTLでmake_unique実装、それはいくつかの答えの後

    を動作するよう 追加質問3

    3.Whatが最高のコンストラクタで、または単に直接

    unique_ptr<Student>{ new Student{1,22.5} } 
    

    をunique_ptrを使用してmake_uniqueを使用している私は、コンストラクタしなさいを定義する必要はありませんとして、後を好むのです。基本的

  • +0

    私は 'make_unique ({1、5.5});'がコンパイルされるべきだと考えています。 –

    +0

    @ manni66 Nah、それは集約初期化の仕組みではありません。 –

    答えて

    1

    残念ながら、make_uniqueは直接リストの初期化を実行しません。あなたはそれを調べる場合は、以下の文が表示されます、説明hereです:

    構築引数argsは、このオーバーロードは、唯一のオーバーロード 決議に参加T.の コンストラクタに渡される非配列型T Tの場合配列型ではありません。 unique_ptrを(新しいT(STD ::前方(引数)...)

    があなたのクラスは、2つの引数を受け取るコンストラクタを持っていない:機能は同等です。これは、しかし、集合体であり、そして2番目の例のように、集計の初期化を使用して構築することができます

    auto* p = new Student{2, 3}; 
    

    しかし、このフォームをmake_unique呼び出していないので、それが失敗した理由です。 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4462.html

    1

    を提案しますstd::make_unique<T>は、その引数をTのコンストラクタに転送します。しかし、クラスStudentには、intdoubleを受け入れるコンストラクタはありません。あなたは1つを追加するかもしれません:

    struct Student 
    { 
        Student(int id, float Score) : id(id), Score(Score) {} 
        int id; 
        float Score; 
    }; 
    

    コードを動作させる。

    +0

    元のstlには何もできないので、コンストラクタを使用しないで、コンストラクタなしで動作します。 –

    +0

    コンストラクタなしで動作しました。unique_ptr {new Student {1,22.5}} –

    +0

    @HariomSingh私はしませんあなたの質問に記載されているように、 'unique_ptr {new student {...}}'を除く他のオプションを見てください。これは集約初期化を使用するので、動作します。しかし、 'std :: make_unique'で集合初期化を使う方法はありません。 –

    関連する問題