C++ 错误:“unique_ptr”不是“std”的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18078153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: ‘unique_ptr’ is not a member of ‘std’
提问by stellarossa
I guess it's pretty self explanatory - I can't seem to use C++11 features, even though I think I have everything set up properly - which likely means that I don't.
我想这是不言自明的 - 我似乎无法使用 C++11 功能,即使我认为我已经正确设置了所有内容 - 这可能意味着我没有。
Here's my code:
这是我的代码:
#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;
}
Here's my version of g++:
这是我的 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.
Here's how I'm compiling the code:
这是我编译代码的方式:
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
Note that I've tried both -std=c++11
and -std=c++0x
to no avail.
请注意,我已经尝试了两者-std=c++11
,-std=c++0x
但都无济于事。
I'm running Ubuntu 12.04 LTS from a flash drive on an Intel x64 machine.
我正在 Intel x64 机器上从闪存驱动器运行 Ubuntu 12.04 LTS。
回答by billz
You need to include header where unique_ptr
and shared_ptr
are defined
您需要在定义unique_ptr
和shared_ptr
定义的位置包含标头
#include <memory>
As you already knew that you need to compile with c++11
flag
正如您已经知道的那样,您需要使用c++11
标志进行编译
g++ main.cpp -o run -std=c++11
// ^
回答by Uzi
So here what I learned in 2020 - memory.h is at /usr/include AND in /usr/include/c++/4.8.5 and you need the second to be found before the first. In Eclipse set the order using Project->Properties->Path and Symbols->Includes->Add... path if needed and set first
因此,我在 2020 年学到的东西 - memory.h 位于 /usr/include AND 位于 /usr/include/c++/4.8.5 中,您需要在第一个之前找到第二个。在 Eclipse 中使用 Project->Properties->Path 和 Symbols->Includes->Add... 路径设置顺序,如果需要并首先设置