C++语言;为什么这个示例代码会出现编译错误??错误代码太长,我无法指定

C++语言;为什么这个示例代码会出现编译错误??错误代码太长,我无法指定,c++,c++11,operator-overloading,smart-pointers,unique-ptr,C++,C++11,Operator Overloading,Smart Pointers,Unique Ptr,我现在正在研究智能指针,我刚刚在书中构建了示例代码。 但当我像下面的代码那样使用unique_ptr时,它会产生编译错误。错误代码太长,几乎都被删掉了,所以我不能把它们全部写下来 我想知道为什么这个代码会出错。。。请帮帮我 编译器和操作系统:g++Ubuntu 9.1.0-2ubuntu2~18.04 9.1.0 我能找到的错误代码 /workspace/What_I_Learned/Cpp/53/53-4.cpp:23:30: error: no match for ‘operator<

我现在正在研究智能指针,我刚刚在书中构建了示例代码。 但当我像下面的代码那样使用unique_ptr时,它会产生编译错误。错误代码太长,几乎都被删掉了,所以我不能把它们全部写下来

我想知道为什么这个代码会出错。。。请帮帮我

编译器和操作系统:g++Ubuntu 9.1.0-2ubuntu2~18.04 9.1.0

我能找到的错误代码

/workspace/What_I_Learned/Cpp/53/53-4.cpp:23:30: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘std::unique_ptr<int>’)
   23 |  cout << "smart pointer 2: " << p2 << '\n';
      |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~
      |       |                         |
      |       std::basic_ostream<char>  std::unique_ptr<int>

/usr/include/c++/9/ostream:691:5: error: no type named ‘type’ in ‘struct std::enable_if<false, std::basic_ostream<char>&>’
我写下的代码

#include <iostream>
#include <memory>

using namespace std;

int main(void) {
    unique_ptr<int> p1(new int(10));
    unique_ptr<int> p2;
    
    cout << "smart pointer 1: " << p1 << '\n';
    cout << "smart pointer 2: " << p2 << '\n';
    cout << "move to p2\n";
    
    p2 = move(p1);
    
    cout << "smart pointer 1: " << p1 << '\n';
    cout << "smart pointer 2: " << p2 << '\n';
    cout << "free memory\n";
    
    p2.reset();
    
    cout << "smart pointer 1: " << p1 << '\n';
    cout << "smart pointer 2: " << p2 << '\n';
}

我已经尝试了-std=g++11和-std=g++14。错误消息告诉您没有运算符错误消息告诉您没有运算符您正在将唯一的\u ptr对象传递给cout,cout不知道如何处理它,因为没有合适的重载可用。如果要打印指针值,则需要如下操作:

std::cout << "P: " << (void *)p1.get() << std::endl;
std::cout << "*P: " << *p1 << std::endl;
如果要打印它所指向的值,当然可以这样做:

std::cout << "P: " << (void *)p1.get() << std::endl;
std::cout << "*P: " << *p1 << std::endl;
但是在p2的情况下,您将取消对无效的nullptr的引用,这将导致未定义的行为。

您正在将唯一的\u ptr对象传递给cout,因为没有合适的重载可用,cout不知道如何处理它。如果要打印指针值,则需要如下操作:

std::cout << "P: " << (void *)p1.get() << std::endl;
std::cout << "*P: " << *p1 << std::endl;
如果要打印它所指向的值,当然可以这样做:

std::cout << "P: " << (void *)p1.get() << std::endl;
std::cout << "*P: " << *p1 << std::endl;

但在p2的情况下,您将取消引用无效的nullptr,这将导致未定义的行为。

没有运算符没有运算符,请提供第一条错误消息,而不是来自编译器的最后一条错误消息(位于屏幕或窗口底部)。这可能更能说明问题的原因。一般来说,当编译器在代码中遇到更多错误时,它会变得更加混乱——这意味着第一个错误之后的错误消息更可能不清楚。在任何情况下,C++ iSoo流都不支持STD::UnQuyJ.PtR的输入或输出。地址还是内容?顺便说一句,哪本书?你想打印指针本身吗?然后使用例如p1.get。或者创建一个@Peter感谢您提供的信息。我想自始至终写下所有的错误,但是太长了,控制台窗口几乎把它减少了80%。。。“我不能粘贴它。@博士,我想打印地址。”。。事实上,这是我在我的国家所学的那本书。与其提供编译器的最后一条错误信息,也就是屏幕或窗口底部的错误信息,不如提供第一条错误信息。这可能更能说明问题的原因。一般来说,当编译器在代码中遇到更多错误时,它会变得更加混乱——这意味着第一个错误之后的错误消息更可能不清楚。在任何情况下,C++ iSoo流都不支持STD::UnQuyJ.PtR的输入或输出。地址还是内容?顺便说一句,哪本书?你想打印指针本身吗?然后使用例如p1.get。或者创建一个@Peter感谢您提供的信息。我想自始至终写下所有的错误,但是太长了,控制台窗口几乎把它减少了80%。。。“我不能粘贴它。@博士,我想打印地址。”。。事实上,这是我在我的国家所学的课程的书。或者p1。如果你对实际指针值感兴趣,请获取。是的,好的,我会加上它。好的,这是在@Devulus答案中:不会加上它。可能是更好的答案。或者p1.get,如果您对实际指针值感兴趣。是的,很好的一点,将添加它。好的,这是在@Devolus answer中:将不会添加它。也许是更好的答案。
#include <iostream>
#include <memory>

int main() 
{
    std::unique_ptr<int> p1(new int( 10 ) );
    std::unique_ptr<char> p2( new char( 'A') );
    
    std::cout << "the value of the smart pointer 1: " << *p1 << '\n';
    std::cout << "the value of the smart pointer 2: " << *p2 << '\n';
}
the value of the smart pointer 1: 10
the value of the smart pointer 2: A
if ( p1 )
{
    std::cout << "the value of the smart pointer 1: " << *p1 << '\n';
}
if ( p2 )
{
    std::cout << "the value of the smart pointer 2: " << *p2 << '\n';
}