C++ shared_ptrコンパイルerror'shared_ptr'was not declared in this scope修復

1303 ワード

#include
#include
#include
#include

using namespace std;

class Simple
{
    public:
        Simple(int p = 0)
        {   
            number = p;
            std::cout << "Simple::" << number << std::endl;
        }   

        ~Simple()
        {   
            std::cout << "~Simple::" << number << std::endl;
        }   

        void PrintSomething()
        {   
            std::cout << "PrintSomething:" << info_extend.c_str() << std::endl;
        }   

        std::string info_extend;
        int number;
};

void TestSharedPtr()
{
    std::shared_ptr my_memory(new Simple(1));
    if(my_memory.get())
    {
        my_memory->PrintSomething();

        my_memory.get()->info_extend = "Additon";
        my_memory->PrintSomething();

        (*my_memory).info_extend += "other";
        my_memory->PrintSomething();
    }

}


int main()
{
    TestSharedPtr();
    return 0;
}

コンパイル:g+-std=c++11-o test.exe ptr.cpp
[jingsia@localhost ~]$ ./test.exe 
Simple::1
PrintSomething:
PrintSomething:Additon
PrintSomething:Additonother
~Simple::1