gpt4 book ai didi

c++ - 动态字符串数组

转载 作者:行者123 更新时间:2023-11-28 07:43:14 24 4
gpt4 key购买 nike

<分区>

我对以下类(class)有疑问。我认为问题出在字符串数组上,因为我制作了另外两个类,问题是一样的。当我运行该程序时,它会抛出“双重自由或腐败”,但我认为任何双重腐败都是不可能的。问题与输入字符串作为引用或作为 Add 方法中的公共(public)参数相同。

class WareH
{
public:
WareH(void)
{
first = true;
rows = 1;
inLine = 0;
cnt = 0;
max = 2;
cL = 0;
strs = new string[max];
}
~WareH(void)
{
delete [] strs;
}
bool Add(string& str, int ending)
{
if (first)
inLine++;
else
cL++;
if (ending == 0)
{
if (first)
first = false;
if (cL != inLine)
return false;
rows++;
}
strs[cnt++] = str;
Bigger();
return true;
}
void Bigger(void)
{
if(max == cnt)
{
max *= 2;
string* tmp = new string[max];
for (int i = 0; i < cnt; i++)
tmp[i] = strs[i];
delete [] strs;
strs = tmp;
}
}
friend ofstream& operator<<(ofstream& of,WareH war)
{
for (int a = 0; a < war.cnt; a++)
of << war.strs[a] << endl;
return of;
}
private:
bool first;
int rows, inLine, cnt, max, cL;
string* strs;
};

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com