【发布时间】:2021-10-31 15:49:11
【问题描述】:
#include<iostream>
#include<string>
#include<memory>
using namespace std;
int main()
{
unique_ptr<int>unPtr1 = make_unique<int>(25);
cout << unPtr1 << endl;//Line 8
cout << *unPtr1 <<endl;
system("pause>nul");
}
在第 8 行显示no operator "<<" matches these operands -- operand types are: std::ostream << std::unique_ptr<int, std::default_delete<int>>。
【问题讨论】:
-
使用
unPtr1.get()获取拥有的指针 -
您的标题提到了存储在 in 唯一指针中的地址,但您的代码尝试将唯一指针本身流式传输。意识到这样的细微差异很重要,因为您的计算机太笨了,无法意识到差异很小。
标签: c++ unique-ptr