C++ shared_ptr 编译 error ‘shared_ptr’ was not declared in this scope修复

来源:互联网 发布:京东返利网站源码 编辑:程序博客网 时间:2024/06/05 16:30
#include<iostream>#include<cstring>#include<string>#include<memory>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<Simple> 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::1PrintSomething:PrintSomething:AdditonPrintSomething:Additonother~Simple::1


0 0
原创粉丝点击