dimanche 5 novembre 2017

Unable to declare dynamic integer array in c++, possible c++11 error

I am new to C++ and am facing a problem with declaring dynamic arrays (of type integer) using the correct syntax. This is my code so far:

#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;
}

And I get this error when I compile and run this code on 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.

I am guessing this has something to do with C++11 syntax maybe? How do I solve this syntax problem? I just want to declare a dynamic integer array of size 5, directly and immediately, without doing this:

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

// and so on... 

Just to clarify, I'm using Atom for Mac (atom.io)

Aucun commentaire:

Enregistrer un commentaire