我一直得到一个期望;错误,我不确定为什么



我写完了代码,但无法弄清楚为什么我得到预期的;错误

我尝试添加 ; 它也期望我,但它会回击其他错误

这是我的代码

int main() {
    int* expandArray(int *arr, int SIZE) { //this is the error line 
    //dynamically allocate an array twice the size
    int *expPtr = new int[2 * SIZE];
    //initialize elements of new array
    for (int i = 0; i < 2 * SIZE; i++) {
        //first add elements of old array
        if (i < SIZE) {
            *(expPtr + i) = *(arr + i);
        }
        //all next elements should be 0
        else {
            *(expPtr + i) = 0;
        }
    }
    return expPtr;
}

}

C++不允许

嵌套函数。你不能在 main(( 中定义函数。

事实上,这是我们可以在C++函数内有函数吗?

您可能希望:

int* expandArray(int *arr, int SIZE) 
{   //this is the error line 
    //dynamically allocate an array twice the size
    int *expPtr = new int[2 * SIZE];
    //initialize elements of new array
    for (int i = 0; i < 2 * SIZE; i++) 
    {
        //first add elements of old array
        if (i < SIZE)
        {
            *(expPtr + i) = *(arr + i);
        }
        //all next elements should be 0
        else 
        {
            *(expPtr + i) = 0;
        }
    }
    return expPtr;
}
int main() 
{
    // call expandArray here
}

相关内容

最新更新