编译器不会使用 -std=c++11 编译智能指针
compiler won't compile smart pointer using -std=c++11
我正在尝试编译一个简单的共享指针声明,但使用 g++ -std=c++11 main.cpp -o main
使用 cmd 但由于某些原因它会抛出一堆错误。我尝试在 Stack Overflow 上找到类似的问题,但 none 符合我的问题。
代码:
std::shared_ptr<int[]>array(new int[100]);
头文件:
#include<iostream>
#include<memory>
编译器版本:g++ (MinGW.org GCC-6.3.0-1) 6.3.0
错误:
In file included from c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr.h:52:0,
from c:\mingw\lib\gcc\mingw32.3.0\include\c++\memory:82,
from main.cpp:2:
c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr_base.h: In instantiation of 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*) [with _Tp1 = int; _Tp = int []; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]':
c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr.h:117:32: required from 'std::shared_ptr<_Tp>::shared_ptr(_Tp1*) [with _Tp1 = int; _Tp = int []]'
main.cpp:7:42: required from here
c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr_base.h:885:39: error: cannot convert 'int*' to 'int (*)[]' in initialization
: _M_ptr(__p), _M_refcount(__p)
^`
我是新手,不知道这个错误是什么意思。
任何建议都会有所帮助。
在 C++17
之前,std::shared_ptr
可以 不能 处理动态分配的数组。升级您的编译器以支持 C++17
,您的代码将顺利编译。
编辑:是早期版本的解决方法。您可以使用:
std::shared_ptr<int> sp(new int[10], custom_deleter<int>{});
其中 custom_deleter
将是一个函数,用于释放分配的内存。在这种情况下,简单的 delete[]
(而不是 shared_ptr
析构函数中的隐式 delete
)就足够了:
template< typename T >
struct custom_deleter
{
void operator ()(const T* arr)
{
delete[] arr;
}
};
但由于您已经在使用 C++11
,可以用 lambda 表达式 替换结构,这将简化代码:
std::shared_ptr<int> sp(new int[10], [](const int* arr){ delete[] arr; });
"cannot convert ..."错误是类型错误,这意味着您使用了错误的数据类型。在这种情况下,它专门告诉您 std::shared_ptr<int[]>
的构造函数需要 int (*)[]
参数类型,但您提供的是 int *
类型的参数。由于编译器不知道如何将 int *
转换为 int (*)[]
,因此您会收到此错误。
无论如何,you shouldn't be using shared_ptr
to manage C-style arrays。我建议切换到 STL 容器,std::array<int, N>
或 std::vector<int>
取决于您是否知道编译时的大小。如果您愿意,可以将任何 STL 容器类型放入智能指针中。
我正在尝试编译一个简单的共享指针声明,但使用 g++ -std=c++11 main.cpp -o main
使用 cmd 但由于某些原因它会抛出一堆错误。我尝试在 Stack Overflow 上找到类似的问题,但 none 符合我的问题。
代码:
std::shared_ptr<int[]>array(new int[100]);
头文件:
#include<iostream>
#include<memory>
编译器版本:g++ (MinGW.org GCC-6.3.0-1) 6.3.0
错误:
In file included from c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr.h:52:0,
from c:\mingw\lib\gcc\mingw32.3.0\include\c++\memory:82,
from main.cpp:2:
c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr_base.h: In instantiation of 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*) [with _Tp1 = int; _Tp = int []; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]':
c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr.h:117:32: required from 'std::shared_ptr<_Tp>::shared_ptr(_Tp1*) [with _Tp1 = int; _Tp = int []]'
main.cpp:7:42: required from here
c:\mingw\lib\gcc\mingw32.3.0\include\c++\bits\shared_ptr_base.h:885:39: error: cannot convert 'int*' to 'int (*)[]' in initialization
: _M_ptr(__p), _M_refcount(__p)
^`
我是新手,不知道这个错误是什么意思。 任何建议都会有所帮助。
在 C++17
之前,std::shared_ptr
可以 不能 处理动态分配的数组。升级您的编译器以支持 C++17
,您的代码将顺利编译。
编辑:是早期版本的解决方法。您可以使用:
std::shared_ptr<int> sp(new int[10], custom_deleter<int>{});
其中 custom_deleter
将是一个函数,用于释放分配的内存。在这种情况下,简单的 delete[]
(而不是 shared_ptr
析构函数中的隐式 delete
)就足够了:
template< typename T >
struct custom_deleter
{
void operator ()(const T* arr)
{
delete[] arr;
}
};
但由于您已经在使用 C++11
,可以用 lambda 表达式 替换结构,这将简化代码:
std::shared_ptr<int> sp(new int[10], [](const int* arr){ delete[] arr; });
"cannot convert ..."错误是类型错误,这意味着您使用了错误的数据类型。在这种情况下,它专门告诉您 std::shared_ptr<int[]>
的构造函数需要 int (*)[]
参数类型,但您提供的是 int *
类型的参数。由于编译器不知道如何将 int *
转换为 int (*)[]
,因此您会收到此错误。
无论如何,you shouldn't be using shared_ptr
to manage C-style arrays。我建议切换到 STL 容器,std::array<int, N>
或 std::vector<int>
取决于您是否知道编译时的大小。如果您愿意,可以将任何 STL 容器类型放入智能指针中。