【问题标题】:Unable to declare dynamic integer array in c++, possible c++11 error无法在 c++ 中声明动态整数数组,可能出现 c++11 错误
【发布时间】:2018-04-17 18:28:39
【问题描述】:

我是 C++ 新手,在使用正确的语法声明动态数组(整数类型)时遇到问题。到目前为止,这是我的代码:

#include <iostream>
using namespace std;

int main() {

    int* pArray(new int[5]{10,20,30,40,50});
    *(pArray + 1) -= 2; //Subtracting 2 from index 1 of pArray
    cout << *pArray << ", " << *(pArray + 1) << endl;

    return 0;
}

当我在 Atom for Mac (atom.io) 上编译和运行此代码时出现此错误:

/Users/pmn/Desktop/myprogram.cpp:6:27: error: expected ')'
int* pArray(new int[5]{10,20,30,40,50});
                      ^
/Users/pmn/Desktop/myprogram.cpp:6:16: note: to match this '('
int* pArray(new int[5]{10,20,30,40,50});
           ^
1 error generated.

我猜这可能与 C++11 语法有关?如何解决这个语法问题?我只想直接并立即声明一个大小为 5 的动态整数数组,而不这样做:

pArray[0] = 10;
pArray[1] = 20;

// and so on... 

澄清一下,我正在使用 Atom for Mac (atom.io)

【问题讨论】:

标签: c++ arrays c++11 dynamic-memory-allocation


【解决方案1】:

正如 StoryTeller 所说,Atom 不是编译器。您的代码运行良好。只需获得一个真正的 c++ 编译器,MinGW p.e.

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多