Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 254
user1779032 Avatar asked Oct 19 '25 14:10

user1779032


2 Answers

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

#include <memory>
like image 187
REMqb Avatar answered Oct 21 '25 16:10

REMqb


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.

like image 23
NEERAJ SWARNKAR Avatar answered Oct 21 '25 16:10

NEERAJ SWARNKAR