【问题标题】:How can I resolve a "'shared_ptr' was not declared in this scope" error?如何解决“未在此范围内声明‘shared_ptr’”错误?
【发布时间】:2015-03-26 10:36:09
【问题描述】:

我正在尝试在 Raspberry Pi 上使用 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;
}

我收到以下错误:

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"));

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

请帮忙。

【问题讨论】:

    标签: c++11 compiler-errors shared-ptr


    【解决方案1】:

    您需要在文件开头添加内存头。

    #include <memory>
    

    【讨论】:

      【解决方案2】:

      如果上述解决方案即使在包含标头后也不起作用,请确保向编译器传递参数--std=c++11

      如果它是用 gcc 编译的,你应该包括头 tr1 头,即 #include 而不是和 std::tr1::shared_ptr 分别。

      【讨论】:

      • GCC 多年来和许多版本(包括几个稳定版本)都不需要 TR1 标头。
      猜你喜欢
      • 2021-01-15
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 2015-06-23
      相关资源
      最近更新 更多