草庐IT

share_open_graph

全部标签

c++ - Qt raw 与 std::shared_ptr

我注意到当在QT中用shared_ptr替换原始指针时,我的代码不再起作用。例如,如果不是QTreeWidgetItem*vItem(newQTreeWidgetItem(ItemTitle));我用std::shared_ptrvItem(newQTreeWidgetItem(ItemTitle));然后,要么程序崩溃,要么什么都没做(即使我使用.get()函数来获取来self代码后面共享的原始指针)。有谁知道可能是什么原因? 最佳答案 对Qt模型项使用共享指针会导致所有权冲突:QTreeWidget获取您传递给它的任何QTree

c++ - 如何在从 Linux 移植的 C++ 项目中使用 Open GL

我刚刚尝试在我的Mac上编译一个开源C++应用程序。我收到错误GL/gl.h文件未找到。我知道这意味着它找不到需要编译和运行的开放式GL库。我对下一步该做什么感到困惑,因为似乎OSX包含对opengl的内置支持。有nothing去下载。对于OSX和Linux上的OpenGL,头文件名称似乎可能不同(OpenGLheadersforOSX&Linux)所以我对下一步该做什么感到困惑。我是否下载OpenGL并将其链接到我的项目?我是否将xcode配置为使用native版本的OpenGL?我要更改标题吗?有人可以提供更多指导吗?这个答案给出了windows/linux的答案——但不是OSX:

c++ - 通过引用传递与通过 shared_ptr 传递

Stackoverflow上的大多数问题都是关于shared_ptr应该按ref还是按值传递。但是我的问题是这样的例子:classFoo;voidfunction1(Foo&ff){ff.m_abc=1024;}voidfunction2(conststd::shared_ptr&ff){ff->m_abc=1024;}function1和function2可能会使用和更改ff的某些部分。我的案例:我需要调用带有参数*this或shared_from_this()的函数。print(msg,*this);orprint(msg,this->shared_from_this());我可以

c++ - 如何使用 Boost Graph Library 更改图中的边权重?

我已经使用Boost图形库定义了一个图形,typedefboost::propertyEdgeWeightProperty;typedefboost::adjacency_listGraph;使用添加边相当简单boost::add_edge(vertice1,vertice2,weight,graph);我还没有弄清楚如何在设置边缘权重后更改它。一种可能的解决方案是删除边缘并使用更新后的权重值重新添加它,但是,这似乎有点过分。 最佳答案 一种解决方案是执行以下操作typedefboost::adjacency_listGraph;t

c++ - std::make_shared 三元返回不编译

我正在使用VisualStudio2010并拥有用于创建抽象基类的两个实现之一的工厂。工厂Create方法采用bool标志并返回shared_ptr中的两个impls之一。使用if语句对我来说效果很好,但是当我尝试使用带有make_shared调用的三元时编译器会报错。classBase{public:Base(){};};classFoo:publicBase{public:Foo(){};};classBar:publicBase{public:Bar(){};};classFactory{public:staticstd::shared_ptrCreate(boolisFoo){

c++ - 一个简单的 std::shared_ptr 构造案例的段错误

来自cppreference我了解到std::shared_ptr有一个构造函数:templateexplicitshared_ptr(Y*ptr);然后我试了一段代码如下:#include#include#includeintmain(void){///block1{std::shared_ptrs1(newstd::string("good"));std::shared_ptrs2(s1.get());///s2std::cerri1(newint(1));std::shared_ptri2(i1.get());///i2std::cerr它会导致block1的段错误,但不会导致b

c++ - 放置指向 shared_ptr 的多重映射的指针不起作用

vector工作正常Headerstd::vector>subnodes_m;DefinitionvoidCompositeSceneNode::AddChild(SceneNode*subnode_p){subnodes_m.emplace_back(subnode_p);}multimap没有Headerstd::multimap>subnodes_m;DefinitionvoidCompositeSceneNode::AddChild(SceneNode*subnode_p,unsignedintlayerIndex){subnodes_m.emplace(layerIndex,

c++ - 为什么这个编译器错误? - 没有匹配函数调用 'std::basic_ofstream<char>::open(std::string&)'

这适用于VisualStudio,并且适用于一台计算机上的GCC4.9.2。但在不同的计算机上,我认为它是相同的GCC4.9.2编译器,但它给了我这个错误。我错过了什么吗?怎么回事?#include#include#includeusingnamespacestd;intmain(){stringfilename;filename="teststring";ofstreamfout;fout.open(filename);fout.||===Build:Debuginfileiotest(compiler:TDM32GNUGCCCompiler4.9.2dw2)===|F:\Users

c++ - 当shared_ptr,当unique_ptr

这个问题在这里已经有了答案:Differencesbetweenunique_ptrandshared_ptr[duplicate](4个答案)关闭7年前。什么时候应该使用shared_ptr什么时候使用unique_ptr?例如在这个类中而不是node*应该是shared_ptr或unique_ptr。它取决于什么?classnode{private:node*parent;vectorchildren;/**txny*x-numerdrzewa*y-numerwezla*/stringid;typeNodetype;//0-term,1-funcpublic:node(node*p

c++ - 如何通过 unsigned long 将 std::shared_ptr 传递给回调?

我有一个旧的C风格库,它使用带有unsignedlong作为用户参数的回调,我想将我的shared_ptr传递给回调,以便增加引用计数。voidcallback(unsignedlongarg){std::shared_ptrptr=???arg???}voidstarter_function(){std::shared_ptrptr=std::make_shared();unsignedlongarg=???ptr???//passtolibrarysoitmaybeusedbycallback}目前我在shared_ptr上使用get(),然后使用C风格的强制转换,但这会在star