霍天青 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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器