在C++中创建非纯数据的unique_ptr
Creating unique_ptr of non plain data in C++
ptr 构造函数处理纯数据,但对对象失败:
#include <string>
#include <memory>
struct MyClass{};
int main() {
std::unique_ptr<int> ptr(new int(5));
std::unique_ptr<std::string> ptr1(new std::string("string")); //function 'ptr1' could not be resolved.
std::unique_ptr<MyClass> ptr2(new MyClass); //unction 'ptr2' could not be resolved.
return 0;
}
为什么?
我的 g++ 版本是 (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04) 4.7.3
实际上,这些初始化工作得很好 - 以下代码编译得很好,例如使用 g++ 4.8.2:
#include <memory>
#include <string>
struct MyClass { };
int main()
{
std::unique_ptr<int> ptr(new int(5));
std::unique_ptr<std::string> ptr1(new std::string("string"));
std::unique_ptr<MyClass> ptr2(new MyClass);
}
您提到它在 eclipse 中失败,因此您可以尝试通过以下方式禁用 eclipse 内部代码验证:
转到项目属性 -> C/C++ 常规 -> 代码分析 ->检查Use project settings
,然后在Syntax and Semantic Errors
取消选中所有错误行。
您可能需要从输出中删除以前的错误,只需选择它们并删除即可。
实际上有一行模式为"函数{0}无法解析",通常 g++ 不会给你这样的错误(我至少从未见过这样的错误)
相关文章:
- 为什么 std::unique 不调用 std::sort?
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 生成"unique"矩阵
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 我对 std::unique(算法)C++有问题
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C++中的指针否定 (!ptr == NULL)
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误