栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

error:‘shared

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

error:‘shared

#1、error: 'shared_ptr' was not declared in this scope

 1 #include 
  2 #include 
  3 using namespace std;
  4 int main()
  5 {
  6   shared_ptr p1 = make_shared();
  7   *p1 = 10;
  8   cout << "p1="<<*p1< p1 = make_shared();
   ^
test2.cpp:6:14: error: expected primary-expression before ‘int’
   shared_ptr p1 = make_shared();
              ^
test2.cpp:7:4: error: ‘p1’ was not declared in this scope
   *p1 = 10;
    ^
root@mkx:~/learn/share_ptr# 


 


#2、因为shared_ptr是C++11的特性


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

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

中文的介绍:
shared_ptr 是C++11提供的一种智能指针类,它足够智能,可以在任何地方都不使用时自动删除相关指针,从而帮助彻底消除内存泄漏和悬空指针的问题。
它遵循共享所有权的概念,即不同的 shared_ptr 对象可以与相同的指针相关联,并在内部使用引用计数机制来实现这一点。

#3、指定C++标准版本就可以了

root@mkx:~/learn/share_ptr# g++  -std=c++11 test2.cpp -o test2
root@mkx:~/learn/share_ptr# ./test2 
p1=10
root@mkx:~/learn/share_ptr# 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033767.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号