如何解决"'shared_ptr'未在此范围内声明"错误?

use*_*032 1 compiler-errors shared-ptr c++11

我正在尝试在Raspberry Pi上编译代码w/shared_ptrs:

#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
   shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
   cout << *message1 <<endl;
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

test.cpp: In function 'int main(int, char**)': test.cpp:4:4: error: 'shared_ptr' was not declared in this scope
    shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
    ^ test.cpp:4:21: error: expected primary-expression before '>' token
    shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
                     ^ test.cpp:4:70: error: 'message1' was not declared in this scope
    shared_ptr<string> message1(new string("Hello Raspberry Pi C++11"));
Run Code Online (Sandbox Code Playgroud)

我用这个命令编译:g++ -std=c++11 -o test test.cpp G ++版本是g ++(Raspbian 4.8.2-21~rpi3rpi1)4.8.2

请帮忙.

小智 8

您需要在文件的开头添加内存标头.

#include <memory>
Run Code Online (Sandbox Code Playgroud)