我有以下类的问题。我认为问题是与字符串数组,因为我做了两个其他类,问题是一样的。当我运行这个程序时,它会抛出“双倍免费或腐败”,但我认为任何双重腐败都是不可能的。问题与输入字符串作为引用或在Add方法中作为常用参数相同。动态字符串数组
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;
};
请重新设置代码的格式。 – filmor 2013-03-14 11:50:48
每当我在同一个句子中听到“动态”和“数组”时,我会考虑'std :: vector'。也许你也应该? – 2013-03-14 11:51:22
学习圣歌... [三条规则...三条规则...三条规则...](http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29) – 2013-03-14 11:53:19