您的位置:首页 > 编程语言 > C语言/C++

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

2017-02-21 10:36 1011 查看
#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::1
PrintSomething:
PrintSomething:Additon
PrintSomething:Additonother
~Simple::1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++
相关文章推荐