我想这是不言自明的 - 我似乎无法使用 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
// ^
关于c++ - 错误 : ‘unique_ptr’ is not a member of ‘std’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18078153/