您的位置:首页 > 编程语言 > C语言/C++

问一个关于c++的问题

2015-01-04 18:51 399 查看
#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了啊,还有,要正常运行如何改?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: