霍天青 2015-06-19 10:35 采纳率: 100%
浏览 1588

为什么会有这个错误?

图片说明
下面是可能发生错误的行截图:
图片说明
图片说明
下面是完整代码的复制,有兴趣可以看一下:
#include
#include
#include
using namespace std;
class strvec{
public:
strvec() :elements(nullptr), first_free(nullptr), cap(nullptr) { };
strvec(const strvec&);
strvec(strvec &&); //移动构造函数
strvec &operator=(const strvec&);
strvec &operator=(strvec &&); //移动赋值运算符
~strvec();
void push_back(const string&);
size_t size() const { return first_free - elements; }
size_t capacity() const { return cap - elements; }
string begin() const { return elements; }
string *end() const { return first_free; }
private:
static allocator alloc; //用来分配空间
void chk_n_alloc() { if (size() == capacity()) reallocate(); }; //保证空间充足
pair<string
, string*> alloc_n_copy(const string*, const string*); //分配内存,并拷贝一个给定范围的元素
void free();
void reallocate(); //分配新内存
string elements;
string *first_free;
string *cap;
};
void strvec::push_back(const string& s)
{
chk_n_alloc();
alloc.construct(first_free++, s);
}
pair<string
, string*> strvec::alloc_n_copy(const string *b, const string *e)
{
auto data = alloc.allocate(e - b);
return{ data, uninitialized_copy(b, e, data) };
}
void strvec::free()
{
if (elements)
{
for (auto p = first_free; p != elements;)
alloc.destroy(--p);
alloc.deallocate(elements, cap - elements);
}
}
strvec::strvec(const strvec &s)
{
auto newdata = alloc_n_copy(s.begin(), s.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
strvec::strvec(strvec &&s)
{
auto first = alloc.allocate(s.end() - s.begin());
auto end = uninitialized_copy(make_move_iterator(s.begin()), make_move_iterator(s.end()), first);
elements = first;
first_free = cap = end;
}
strvec& strvec::operator=(const strvec &rhs)
{
auto newdata = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = newdata.first;
first_free = cap = newdata.second;
return *this;
}
strvec& strvec::operator=(strvec &&s)
{
if (this != &s)
{
free();
auto first = alloc.allocate(s.end() - s.begin());
auto end = uninitialized_copy(make_move_iterator(s.begin()), make_move_iterator(s.end()), first);
elements = first;
first_free = cap = end;
}
return *this;
}
void strvec::reallocate()
{
auto newcapacity = size() ? 2 * size() : 1;
auto newdata = alloc.allocate(newcapacity);
auto dest = newdata;
auto elem = elements;
for (size_t i = 0; i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
first_free = dest;
cap = elements + newcapacity;
}
int main(void)
{
strvec s;
string temp;
while (cin >> temp)
{
s.push_back(temp);
cout << s.size() << endl;
}
}

  • 写回答

1条回答

  • oyljerry 2015-06-20 15:29
    关注

    你的编译器支持移动构造语法吗

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决