lundi 29 juin 2015

C++: new[] operator

#include <bits/stdc++.h>
#define _ ios::sync_with_stdio(0); cin.tie(0);
using namespace std;

int main(){_
    auto arr = new int[5];
    // int arr[5] = {1, 2, 3, 4, 5};
    for (auto i: arr){
        cout << i << ' ';
    }
}

Why isn't this working? I am getting a compile time error saying this.

C.cpp: In function 'int main()':
C.cpp:8:15: error: 'begin' was not declared in this scope
  for (auto i: arr){
               ^
C.cpp:8:15: note: suggested alternatives:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/x86_64-pc-cygwin/bits/stdc++.h:94:0,
                 from C.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note:   'std::begin'
     begin(const valarray<_Tp>& __va)
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note:   'std::begin'
C.cpp:8:15: error: 'end' was not declared in this scope
  for (auto i: arr){
               ^
C.cpp:8:15: note: suggested alternatives:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/x86_64-pc-cygwin/bits/stdc++.h:94:0,
                 from C.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1226:5: note:   'std::end'
     end(const valarray<_Tp>& __va)
     ^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1226:5: note:   'std::end'

When I initialized array in commented way, it works fine. (obviously) So, I think problem is with new operator. But I don't understand what it is.

Aucun commentaire:

Enregistrer un commentaire