C++ unique_ptr;为什么这个示例代码会出现编译错误??错误代码太长,我无法指定
C++ unique_ptr; Why this sample codes get compile error?? error codes are so long that I can't specify it
我现在正在研究智能指针,书上的示例代码也是自己搭建的。
但是当我像下面这段代码那样使用 unique_ptr
时,它会导致编译错误。错误码长的都快被剪掉了,没法全部写下来
我想知道为什么这段代码出错...请帮助我。
compiler and OS : 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
。
错误消息告诉您 std::unique_ptr
没有 operator <<
重载。你可以做的是
std::cout << "smart pointer dereferenced: " << *p1 << '\n';
请注意,如果智能指针处于空状态,这是未定义的行为。
作为旁注,请考虑使用 std::make_unique<int>(42)
初始化 std::unique_ptr
(如果 C++14 可用),因为它封装了原始 new
.
您正在将 unique_ptr
对象传递给 cout
,它不知道如何处理它,因为没有合适的重载可用。如果你想打印指针值,那么你需要这样做:
std::cout << "P: " << (void *)p1.get() << std::endl;
如果你想打印它指向的值,你当然可以这样做:
std::cout << "*P: " << *p1 << std::endl;
但在 p2
的情况下,您将取消引用 nullptr
,这是无效的,会导致未定义的行为。
std::unique_otr
类型的对象没有 operator <<
。
如果需要输出拥有指针的值那么写
cout << "smart pointer 1: " << p1.get() << '\n';
如果class模板std::unique_ptr
的模板参数是char
那么需要将成员函数get
返回的表达式转换为类型void *
。例如
cout << "smart pointer 1: " << static_cast<void *>( p1.get() ) << '\n';
这是一个演示程序。
#include <iostream>
#include <memory>
int main()
{
std::unique_ptr<int> p1(new int( 10 ) );
std::unique_ptr<char> p2( new char( 'A') );
std::cout << "smart pointer 1: " << p1.get() << '\n';
std::cout << "smart pointer 2: " << static_cast<void *>( p2.get() ) << '\n';
}
程序输出可能看起来像
smart pointer 1: 0x5593824d6e70
smart pointer 2: 0x5593824d6e90
如果 std::unique_ptr<char>
类型的对象的拥有指针在没有转换的情况下输出,那么它将作为 C 字符串输出,而不是输出其存储的值(地址)。
如果你需要输出class模板对象的指向值std::unique_ptr
那么只需要应用解引用运算符。
例如
cout << "smart pointer 1: " << *p1 << '\n';
这是一个演示程序
#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
在输出指向值之前,您可以检查class模板std::unique_ptr
的对象是否没有像
那样存储空指针
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';
}
我现在正在研究智能指针,书上的示例代码也是自己搭建的。
但是当我像下面这段代码那样使用 unique_ptr
时,它会导致编译错误。错误码长的都快被剪掉了,没法全部写下来
我想知道为什么这段代码出错...请帮助我。
compiler and OS : 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
。
错误消息告诉您 std::unique_ptr
没有 operator <<
重载。你可以做的是
std::cout << "smart pointer dereferenced: " << *p1 << '\n';
请注意,如果智能指针处于空状态,这是未定义的行为。
作为旁注,请考虑使用 std::make_unique<int>(42)
初始化 std::unique_ptr
(如果 C++14 可用),因为它封装了原始 new
.
您正在将 unique_ptr
对象传递给 cout
,它不知道如何处理它,因为没有合适的重载可用。如果你想打印指针值,那么你需要这样做:
std::cout << "P: " << (void *)p1.get() << std::endl;
如果你想打印它指向的值,你当然可以这样做:
std::cout << "*P: " << *p1 << std::endl;
但在 p2
的情况下,您将取消引用 nullptr
,这是无效的,会导致未定义的行为。
std::unique_otr
类型的对象没有 operator <<
。
如果需要输出拥有指针的值那么写
cout << "smart pointer 1: " << p1.get() << '\n';
如果class模板std::unique_ptr
的模板参数是char
那么需要将成员函数get
返回的表达式转换为类型void *
。例如
cout << "smart pointer 1: " << static_cast<void *>( p1.get() ) << '\n';
这是一个演示程序。
#include <iostream>
#include <memory>
int main()
{
std::unique_ptr<int> p1(new int( 10 ) );
std::unique_ptr<char> p2( new char( 'A') );
std::cout << "smart pointer 1: " << p1.get() << '\n';
std::cout << "smart pointer 2: " << static_cast<void *>( p2.get() ) << '\n';
}
程序输出可能看起来像
smart pointer 1: 0x5593824d6e70
smart pointer 2: 0x5593824d6e90
如果 std::unique_ptr<char>
类型的对象的拥有指针在没有转换的情况下输出,那么它将作为 C 字符串输出,而不是输出其存储的值(地址)。
如果你需要输出class模板对象的指向值std::unique_ptr
那么只需要应用解引用运算符。
例如
cout << "smart pointer 1: " << *p1 << '\n';
这是一个演示程序
#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
在输出指向值之前,您可以检查class模板std::unique_ptr
的对象是否没有像
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';
}