How can I resolve a "'shared_ptr' was not declared in this scope" error?

12,587

Solution 1

You need to add the memory header at the beginning of your file.

#include <memory>

Solution 2

if above solution does not work even after include of header, please ensure that to compiler you are passing argument --std=c++11

In case, it is being compiled with gcc, you should include header tr1 headers i.e. #include instead of and std::tr1::shared_ptr respectively.

Share:
12,587
user1779032
Author by

user1779032

Updated on June 05, 2022

Comments

  • user1779032
    user1779032 almost 2 years

    I'm trying to compile code w/ shared_ptrs on Raspberry Pi:

    #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;
    }
    

    I get the following error:

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

    I'm compiling with this command: g++ -std=c++11 -o test test.cpp G++ version is g++ (Raspbian 4.8.2-21~rpi3rpi1) 4.8.2

    Please help.

  • Konrad Rudolph
    Konrad Rudolph over 7 years
    GCC hasn’t needed the TR1 headers for years and many versions (including several stable releases).