草庐IT

File_ptr

全部标签

c++ - union 中的 Shared_ptr

我想访问union的共享指针,尽管发生段错误:structunion_tmp{union_tmp(){}~union_tmp(){}union{inta;std::shared_ptr>ptr;};};intmain(){union_tmpb;std::shared_ptr>tmp(newstd::vector);b.ptr=tmp;//heresegmentationfaulthappensreturn0;}错误的原因是什么,我该如何避免? 最佳答案 您需要在union体中初始化std::shared_ptr:union_tmp(

c++ - 如何从 boost::ptr_vector 中删除元素

所以我试图通过使用boost::ptr_vector摆脱我的std::vector。现在我试图从一个元素中删除一个元素,并删除已删除的元素。对我来说最明显的事情是:classA{intm;};boost::ptr_vectorvec;A*a=newA;vec.push_back(a);vec.erase(a);但这甚至无法编译(完整的错误信息见下文)。我已经像在std::vector上一样尝试了删除/删除习惯用法,但boost::ptr_vector的所有算法结果都与std::vector中的算法略有不同。所以我的问题:如何从ptr_vector中删除指针?我是否还需要手动delete

c++ - 如何转移 boost::ptr_vector 元素的所有权?

#include#includeusingnamespacestd;usingnamespaceboost;structA{~A(){coutv;v.push_back(newA);A*temp=&v.front();v.release(v.begin());deletetemp;return0;}输出:deleted0x300300deleted0x300300c(6832)malloc:***errorforobject0x300300:doublefree 最佳答案 ptr_vector::release返回ptr_vecto

c++ - 如何防止任何人窃取我的 shared_ptr?

因此,我使用boost::shared_ptr来获得它提供的所有各种引用计数优势——显然是初学者的引用计数,还有复制、分配并因此存储在STL容器中的能力。问题是,如果我只将它传递给一个“恶意”函数或对象,该对象可以保存ptr,然后如果外部函数或对象没有很好地放弃其所有权,我将永远无法取消分配它.最终,我尽量明确对象所有权。我通过让所有者保留对象的唯一shared_ptr来实现这一点,而“guest”对象仅将weak_ptr存储到对象。我真的不想要shared_ptr的“共享”部分,但我必须使用shared_ptr才能生成weak_ptr。我想使用scoped_ptr,但它非常有限,因为

c++ - 在构造函数中初始化 Boost shared_ptr

我有一个类,它有一个boost::asio::io_service对象。我希望这个对象存储在boost::shared_ptr中。所以我的标题看起来像这样(我去掉了所有不必要的代码,这样就不会分散注意力)classCommandDispatcher{private:boost::shared_ptrm_ioservice;public:CommandDispatcher();};当我现在创建CommandDispatcher的对象时,我希望为指针初始化一个io_service对象。现在我不太清楚该怎么做。我查找了两种不同的解决方案,但只有一种有效,我不太确定它是否合适。但是你自己看看:

c++ - 链接 : fatal error LNK1181: cannot open input file 'libclamav.lib'

我正在使用MicrosoftVisualStudio2010,我正在使用开源Clamav,我的代码在下面给出,它产生了一个错误#include#include#include#include#include#include#include#includeintmain(intargc,char**argv){intfd,ret;unsignedlongintsize=0;unsignedintsigs=0;longdoublemb;constchar*virname;structcl_engine*engine;if(argc!=2){printf("Usage:%sfile\n",a

c++ - Qt错误: LNK1181: cannot open input file 'debug\main.obj'

Qtcreator运行良好,但突然出现问题error:LNK1181:cannotopeninputfile'debug\main.obj'。对于任何类型的应用程序,无论是GUI还是console,这个问题总是会出现。Qt卸载了,重新安装,问题依旧。我没有在QtCreator设置中做任何事情,我保留了默认设置。下面的应用程序是简单的控制台应用程序,它会出现同样的问题。//main.cpp#includeintmain(intargc,char*argv[]){QCoreApplicationa(argc,argv);returna.exec();}注意:我使用的是适用于Windows3

c++ - 为什么 unique_ptr 有一个 nullptr_t 构造函数?

我不清楚有什么好处。如果我有:Foo*foo=nullptr;std::unique_ptrunique_foo(foo);在那种情况下是否调用了nullptr_t构造函数?或者仅当您这样做时:std::unique_ptrunique_foo(nullptr);谢谢!有一些讨论here这是为了允许你传入nullptr_t,否则它不会编译,因为它不会转换为类型指针。所以我的问题可能是为什么它不转换? 最佳答案 一个可能的原因是采用unique_ptr::pointer参数的unique_ptr构造函数是显式。这意味着在没有uniqu

c++ - std::unique_ptr 是 RAII 的应用吗?

这是对它的准确描述吗?有道理吗?您是否保证在unique_ptr超出范围之前它指向的对象不会被删除[即使您没有使用unique_ptr]? 最佳答案 是的,std::unique_ptr遵循RAII设计原则。不,std::unique_ptr不会阻止其他代码做一些愚蠢的事情,比如在属于unique_ptr的指针上调用delete>。unique_ptr本身将在其拥有的对象上调用deleter1,当出现以下任一情况时:超出范围或unique_ptr被重新分配(通过operator=或reset)以指向不同的对象还可以通过移动到不同的智

ssh报错:no such identity: /xxx/xxx/.ssh/id_rsa: No such file or directory解决方案

ssh报错:nosuchidentity:/xxx/xxx/.ssh/id_rsa:Nosuchfileordirectory.Permissiondenied(publickey)解决方案最近在使用ssh方式连接公司跳板机时报错:Warning:Permanentlyadded'xxx'(ECDSA)tothelistofknownhosts.nosuchidentity:/xxx/xxx/.ssh/id_rsa:Nosuchfileordirectorynosuchidentity:/xxx/xxx/.ssh/id_dsa:Nosuchfileordirectorynosuchidenti