#include<iostream>
#include<string>
#include<vector>
#include<boost\shared_ptr.hpp>
using namespace std;
class strblodptr
{
public:
strblodptr():curr(0){}
strblodptr(weak_ptr<vector<string>>v,size_t t=0):wptr(v),curr(t){}
string&deref()const;
strblodptr&incr();
private:
size_t curr;
shared_ptr<vector<string>>check(size_t,const string&)const;
weak_ptr<vector<string>>wptr;
};
shared_ptr<vector<string>>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<string>v(strs,strs+4);
vector<string>*vp=new vector<string>();
*vp=v;
shared_ptr<vector<string>>sp(vp);
weak_ptr<vector<string>>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了啊,还有,要正常运行如何改?