【发布时间】:2014-02-28 01:09:01
【问题描述】:
void RetailerOrder::addItem(Product* p)
{
bool space = false;
int counter = 0;
while ((space == false) && (counter < manifest.size()))
{
if (manifest[counter] == nullptr);
{
manifest[counter] = p;
space = true;
}
counter++;
}
if (space == false)
{
cout << "no space" << endl;
}
}
为什么每次通过 while 循环时计数器都会重置为零?如果我按原样使用它,只有我输入的最后一个产品会存储在数组中,因为i 始终为 1。有没有办法让计数器增加。
【问题讨论】:
-
if(manifest[counter == nullptr);末尾的分号可能与此有关。 -
谢谢!这太简单了,我觉得自己很愚蠢,哈哈
-
这就是为什么你应该使用
space = true;而不是goto fail;:-)