草庐IT

c++ - eclipse 火星 : Symbol 'unique_ptr' could not be resolved

使用EclipseMars,我遇到了Symbol'unique_ptr'couldnotberesolved错误。我尝试将-std=c++11添加到CDTGCC内置编译器设置,但这没有帮助。当我重新打开Eclipse时错误消失了,但是如果我修改代码,错误又回来了。一个简单的代码示例:std::unique_ptrp1; 最佳答案 在EclipseMars中打开Window>Preferences>C/C++>Build>Settings>Discovery>CDTGCCBuild-inCompilerSettings将-std=c+

c++ - 为什么 "error: invalid application of ' sizeof' 为使用 unique_ptr 的不完整类型”通过添加空析构函数来修复?

这个问题在这里已经有了答案:Isstd::unique_ptrrequiredtoknowthefulldefinitionofT?(9个回答)关闭7年前。我在类里面拉皮条STFT.在header中用这个编译就好了:classSTFT;//pimplofftopreventpointnameclashclassWhatever{private:STFT*stft;这在实现中:#include"STFT.h"Whatever::Whatever():stft(newSTFT()){//blahblah}Whatever::~Whatever(){deletestft;//pureevil

c++ - 我应该使用 QScopedPointer 还是 std::unique_ptr?

我正在开始一个新项目,使用Qt5和QMAKE_CXXFLAGS+=-std=c++1y。我不确定我应该更喜欢QScopedPointer还是std::unique_ptr。我读了somewhereQScopedPointer不再那么酷了。QScopedPointer是否有unique_ptr缺少的功能?在替换QScopedPointer时,是否有任何我不想使用的unique_ptr功能?还是相反? 最佳答案 QScopedPointer严格弱于unique_ptr因为它不支持移动语义。它的功能在其他方面非常相似。移动语义非常有用,不

c++ - 传递给方法时,我应该将 shared_ptr 转换为 weak_ptr 吗?

关于这个主题已经有几个问题,但我仍然不确定该怎么做:我们的代码库在很多地方使用shared_ptr。不得不承认,我们在写的时候并没有明确定义所有权。我们有一些方法,比如voiddoSomething(shared_ptrptr){//doSomething()isamemberfunctionofaclass,butusuallywon'tstoretheptrptr->foo();...}在发现第一个(间接的)循环依赖后,我想纠正我们设计中的错误。但我不确定如何。将上面的方法更改为有什么好处吗?voiddoSomething(weak_ptrptr){shared_ptrptrSha

c++ - 在一组 shared_ptr 中找到一个值

我有一组shared_ptr并想在其中找到一个值:typedefstd::shared_ptrIntPtr;structCompare{booloperator()(constIntPtr&a,constIntPtr&b){return*as;autox=std::make_shared(3);s.insert(x);boolfound=s.find(std::make_shared(3))!=s.end();它可以工作,但效率不高-每次尝试查找值时都需要新的临时指针。还有其他办法吗?看起来像Searchinginasetofshared_ptr有一些可能有用的想法吗?

c++ - "error: use of deleted function"在 move 构造函数中对 unique_ptr 调用 std::move 时

#includeclassA{public:A(){}A(constA&&rhs){a=std::move(rhs.a);}private:std::unique_ptra;};此代码无法使用g++4.8.4编译并抛出以下错误:error:useofdeletedfunction‘std::unique_ptr&std::unique_ptr::operator=(conststd::unique_ptr&)[with_Tp=int;_Dp=std::default_delete]’a=std::move(rhs.a);^我知道unique_ptr的复制构造函数和复制赋值构造函数已删除

c++ - 如何解决 boost::shared_ptr 和使用 std::shared_ptr 之间的冲突?

如果我在此代码段中从boost::shared_ptr更改为std::shared_ptr,我将收到链接器错误。#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include//usingnamespacestd;//usingnamespaceboost;usingstd::string;usingstd::ostringstre

c++ - 从函数返回的 unique_ptr 的范围是什么?

这能正常工作吗?(见示例)unique_ptrsource(){returnunique_ptr(newA);}voiddoSomething(A&a){//...}voidtest(){doSomething(*source().get());//unsafe?//Whendoesthereturnedunique_ptrgooutofscope?} 最佳答案 从函数返回的unique_ptr没有范围,因为范围只适用于名称。在您的示例中,临时unique_ptr的生命周期以分号结束。(所以是的,它会正常工作。)一般来说,当完整表达

c++ - 为什么 make_unique 不能与 unique_ptr::reset 一起使用?

我尝试用VS2013编译一些C++代码,unique_ptr::reset()似乎不适用于make_unique();一个小的可编译重现代码片段如下:#includeusingnamespacestd;intmain(){unique_ptrp=make_unique(3);p.reset(make_unique(10));}从命令行编译:C:\Temp\CppTests>cl/EHsc/W4/nologotest.cpp这些是来自MSVC编译器的错误:test.cpp(6):errorC2280:'voidstd::unique_ptr>::reset>>(_Ptr2)':attem

c++ - 如何在必须复制构造的类中使用 std::auto_ptr?

我的foo类包含一个std::auto_ptr成员,我想复制它的构造,但这似乎是不允许的。作业也有类似的事情。请参阅以下示例:structfoo{private:int_a;std::string_b;std::auto_ptr_c;public:foo(constfoo&rhs):_a(rhs._a),_b(rhs._b),_c(rhs._c)//error:Cannotmutaterhs._ctogiveupownership-D'Oh!{}foo&operator=(constfoo&rhs){_a=rhs._a;_b=rhs._b;_c=rhs._c;//error:Samep