私はちょうど 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を使用して操作できるようにする方法
ですSTLでmake_unique実装、それはいくつかの答えの後
を動作するよう 追加質問3
3.Whatが最高のコンストラクタで、または単に直接
unique_ptr<Student>{ new Student{1,22.5} }
をunique_ptrを使用してmake_uniqueを使用している私は、コンストラクタしなさいを定義する必要はありませんとして、後を好むのです。基本的
私は 'make_unique({1、5.5});'がコンパイルされるべきだと考えています。 –
@ manni66 Nah、それは集約初期化の仕組みではありません。 –