有关智能指针的代码失败
codes about smart pointer failed
我刚刚在c++ Primer中尝试了智能指针的代码,我没有发现任何问题。这和书中的密码非常相似。
代码:
#include <vector>
#include <memory>
std::shared_ptr<std::vector<int>> *my_malloc() {
return std::make_shared<std::vector<int>>();
}
编译器错误:
12_7.cc:6:12: error: no viable conversion from
'typename enable_if<!is_array<vector<int, allocator<int> > >::value, shared_ptr<vector<int, allocator<int> > > >::type'
(aka 'std::__1::shared_ptr<std::__1::vector<int, std::__1::allocator<int> > >')
to
'std::shared_ptr<std::vector<int> > *'
return std::make_shared<std::vector<int>>();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
您的my_malloc
函数声明它返回指向共享指针的指针(这很奇怪,可能是错误的),然后您返回共享指针(就像您应该的那样)。试着
std::shared_ptr<std::vector<int>> my_malloc() {
return std::make_shared<std::vector<int>>();
}
std::shared_ptr<std::vector<int>> *
表示指向的指针,指向整型向量的共享指针。您正在尝试从应该返回shared_ptr*
的函数返回shared_ptr
。
您可能希望您的函数返回一个实际的shared_ptr
,而不是一个(非共享的)指针,因此只需删除*
。
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 链接器命令失败,macOS 上的退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- Qt:更新 QString 时自动生成的代码失败
- 使用 execv 从C++代码执行的 Linux 脚本失败
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 为什么此代码导致未处理的异常失败
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 查找在 4 和问题中代码失败的测试用例
- 多个输入的代码失败
- MSDN 站点上调用 AdjustTokenPrivileges() 的示例代码失败,并出现"Cannot enable the SE_LOCK_MEMORY_NAME privilege"
- 将C++HelloWorld国际象棋引擎代码转换为C代码失败
- 代码失败在向量的数据之前插入数字
- 编译包含动态并行性的代码失败
- 编译使用 libcxx 的套接字函数 bind() 的代码失败
- 发布版本导致简单的protobuf代码失败
- 使用opencv调试代码失败,并出现分段错误
- 有关智能指针的代码失败
- 井字游戏代码失败