错误:“unique_ptr”不是“std”的成员
error: ‘unique_ptr’ is not a member of ‘std’
我想这是不言自明的 - 我似乎无法使用 C++11 功能,即使我认为我已经正确设置了所有内容 - 这可能意味着我没有。
这是我的代码:
#include <cstdlib>
#include <iostream>
class Object {
private:
int value;
public:
Object(int val) {
value = val;
}
int get_val() {
return value;
}
void set_val(int val) {
value = val;
}
};
int main() {
Object *obj = new Object(3);
std::unique_ptr<Object> smart_obj(new Object(5));
std::cout << obj->get_val() << std::endl;
return 0;
}
这是我的 g++ 版本:
ubuntu@ubuntu:~/Desktop$ g++ --version
g++ (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
以下是我编译代码的方式:
ubuntu@ubuntu:~/Desktop$ g++ main.cpp -o run --std=c++11
main.cpp: In function ‘int main()’:
main.cpp:25:2: error: ‘unique_ptr’ is not a member of ‘std’
main.cpp:25:24: error: expected primary-expression before ‘>’ token
main.cpp:25:49: error: ‘smart_obj’ was not declared in this scope
请注意,我已经尝试了-std=c++11
和-std=c++0x
都无济于事。
我正在从英特尔 x64 机器上的闪存驱动器运行 Ubuntu 12.04 LTS。
您需要在定义unique_ptr
和shared_ptr
的地方包含标头
#include <memory>
正如您已经知道的那样,您需要使用c++11
标志进行编译
g++ main.cpp -o run -std=c++11
// ^
所以这是我在 2020 年学到的 - memory.h 位于/usr/include/c++/4.8.5 中的/usr/include AND 中,您需要在第一个之前找到第二个。在 Eclipse 中,使用 Project->Properties->Path 和 Symbols->Include->Add...路径(如果需要(并首先设置
你需要
包含可以解决问题 #include,至少在我的ubunto Linux机器上
相关文章:
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- std::to_string - 'to_string' 不是 'std' 的成员 - Visual Studio Code 1.42.0
- FreeFileSync C++错误:'byte'不是 'std' 的成员
- 使用 std::addressof(std::cout) 而不是 &std::cout 是否有任何风险?
- 隐式转换为比较函数对象(函子)用于 std::sort 而不是 std::map?
- 无法从好友功能访问类的私有成员?"ostream"不是"std"的成员?
- 线程不是 std c++ 的成员
- 为什么会出现错误:size不是std的成员
- 读取互斥对象范围之外的volatile变量,而不是std::atomic
- 不是 'std::vector<_Ty> 的成员
- C++ GCC 错误:"sqrtl"不是"std"的成员
- _Pairib":不是"std::map,std::分配器"的成员>>
- 如何将 std::list of 派生类而不是 std::list of parent class 传递给函数
- 'make_unique'不是'std'的成员
- "random_shuffle":不是"std"错误的成员
- 为什么错误:"locate_zone"不是"std::chrono"的成员
- 为什么 std::when_any 使用 std::tuple 而不是 std::vector 作为其结果类型?
- "<某些系统标头>:错误:"<在此处插入函数>"在移动源文件后不是"std"的成员
- Clang希望我写:: STD :: Declval而不是STD :: Declval
- 哈希输出为 BYTE 而不是 std::string