クラス内のオブジェクトへのポインターのベクトルが必要です。std::unique_ptr
クラスでオブジェクトが作成/所有/破棄されるため、デストラクタを作成しないようにするために使用したかったのですが、理解できないコンパイラエラーがあります。次のコードは、私の問題の短いサンプルとして機能します。
std::unique_ptr<int> createPtr(int value)
{
std::unique_ptr<int> ptr(new int(value));
return ptr;
};
int main()
{
std::vector<std::unique_ptr<int>> vec;
vec.push_back(createPtr(1));
std::unique_ptr<int> ptr = createPtr(2);
vec.push_back(ptr);//error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
}
このエラーが発生する理由と、 の正しい使用法を教えてくださいstd::unique_ptr
。