ccArtermices
本帖最后由

本帖最后由 ccArtermices 于 2015-1-4 18:57 编辑

不知道怎么了,下面再发一次代码

#include *本站禁止HTML标签噢*

#include *本站禁止HTML标签噢*

#include *本站禁止HTML标签噢*

#include<boost\shared_ptr.hpp>

using namespace std;

class strblodptr

{

public:

strblodptr():curr(0){}

strblodptr(weak_ptr<vector *本站禁止HTML标签噢* >v,size_t t=0):wptr(v),curr(t){}

string&deref()const;

strblodptr&incr();

private:

size_t curr;

shared_ptr<vector *本站禁止HTML标签噢* >check(size_t,const string&)const;

weak_ptr<vector *本站禁止HTML标签噢* >wptr;

};

shared_ptr<vector *本站禁止HTML标签噢* >strblodptr::check(size_t t,const string& str)const

{

auto ref=wptr.lock();

if(!ref)throw runtime_error("111111111111111111");

if(t>=ref->size())throw out_of_range(str);

return ref;

}

string&strblodptr::deref()const

{

auto p=check(curr,"dereference past end");

return (*p)[curr];

}

strblodptr&strblodptr::incr()

{

check(curr,"increment past end of strblodptr");

++curr;

return *this;

}

void main()

{

string strs[]={"My","name","is","cc"};

vector *本站禁止HTML标签噢* v(strs,strs+4);

vector *本站禁止HTML标签噢* *vp=new vector *本站禁止HTML标签噢* ();

*vp=v;

shared_ptr<vector *本站禁止HTML标签噢* >sp(vp);

weak_ptr<vector *本站禁止HTML标签噢* >wp(sp);

strblodptr ss(wp,3);

cout<<ss.deref()<<endl;

ss=ss.incr();

cout<<ss.deref()<<endl;

delete vp;

}

为什么编译器说out of range at memory?我有delete了啊,还有,要正常运行如何改?