[英]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
都無濟於事。
我正在 Intel 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 AND 位於 /usr/include/c++/4.8.5 中,您需要在第一個之前找到第二個。 在 Eclipse 中使用 Project->Properties->Path 和 Symbols->Includes->Add... 路徑設置順序,如果需要並首先設置
您需要包含 #include 來解決問題,至少在我的 ubunto linux 機器上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.