C++ 函数不会执行多次

标签 c++ arrays function

我试过环顾四周,但在任何地方都找不到任何相关信息。

我正在编写一个带有“推送”功能的自定义数组类,以向数组添加一个值。 它似乎工作得很好,但不会执行多次。

以下面的main方法为例:

int main()
{
    Array<int> test(4,5);
    test.push(4);

    test.writeOrdered("Output.txt");
    return 0;
}

这会将 int 值 4 放入数组中第一个可用位置并执行 writeOrdered 函数。

另一方面,以下主要方法:

int main()
{
    Array<int> test(4,5);
    test.push(4);
    test.push(5);

    test.writeOrdered("Output.txt");
    return 0;
}

这会将数字 4 放入数组中的第一个可用点,如上,然后停止。它不会再执行任何代码行。

下面是push函数供引用:

void push(Datatype p_item)
{
    bool inserted = false;
    int i = 0;

    while (inserted == false)
    {
        if (m_array[i] < 0)
        {
            m_array[i] = p_item;
            i++;
            inserted = true;
            cout << p_item << " saved to array" << endl;
            system("pause");
        }
    }
}

最佳答案

你有一个无限循环。在第一次插入 m_array[0] >= 0 之后,i 永远不会增长。如果您以某种方式调试代码,您就会发现它。

关于C++ 函数不会执行多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22201977/

相关文章:

python - 创建一个可以访问参数和函数的装饰器

c++ - 没有库函数复制字符矩阵(char**)

c++ - 奇怪的 cython 扩展崩溃,cython 错误?

arrays - Ruby Koans - 数组填充方法会影响它的返回方式

c - C语言中删除字符串中的字母

php - 更改函数以使用 preg_replace() 而不是 ereg_replace

python - 在 python 中创建一个确认函数

c++ - 尝试将字符串推到列表后面时出现段错误

c++ - 模式名称/写得好吗?

PHP - 从面包屑列表的多维关联数组返回父数组