【发布时间】:2017-05-25 11:38:35
【问题描述】:
试图创建一个 shared_ptr 到 int 的向量。
我哪里错了?谢谢。基思:^)
#include <iostream>
#include <vector>
#include <memory>
int main() {
std::vector<std::shared_ptr<int> > w;
std::vector<std::shared_ptr<int> >::iterator it_w;
w.push_back(new int(7));
std::cout << std::endl;
}
编译结果:
pickledegg> g++ -std=c++11 -o shared_ptr shared_ptr.cpp
shared_ptr.cpp:29:4: error: no matching member function for call to 'push_back'
w.push_back(new int(7));
~~^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:697:36: note:
candidate function not viable: no known conversion from 'int *' to 'const value_type' (aka
'const std::__1::shared_ptr<int>') for 1st argument
_LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:699:36: note:
candidate function not viable: no known conversion from 'int *' to 'value_type' (aka
'std::__1::shared_ptr<int>') for 1st argument
_LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
^
1 error generated.
【问题讨论】:
标签: c++ c++11 shared-ptr smart-pointers