Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: spartan on November 07, 2017, 09:35:26 am

Title: For each loop
Post by: spartan on November 07, 2017, 09:35:26 am
I'm revising C++ and I got error, but I'm pretty sure this passed in Microsoft Visual Studio, is my syntax really incorrect or is this just Code::Blocks specific issue? Because I wrote for loop just as is in tutorial(version that is commented out works).

int* returnArray(int size)
{
    return new int[size];
}


int main()
{
    int *myarray = returnArray(10);
    for(auto const &index : myarray)
        std::cout << index;
//    for(int i = 0; i < 10; i++)
//        std::cout << myarray << "\n";
    delete[] myarray;
    return 0;
}

Build log:

-------------- Build: Debug in exercise (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -std=c++14  -c /home/uros/Namizje/CodeblocksProjects/exercise/main.cpp -o obj/Debug/main.o
/home/uros/Namizje/CodeblocksProjects/exercise/main.cpp: In function ‘int main()’:
/home/uros/Namizje/CodeblocksProjects/exercise/main.cpp:63:29: error: ‘begin’ was not declared in this scope
     for(auto const &index : myarray)
                             ^
/home/uros/Namizje/CodeblocksProjects/exercise/main.cpp:63:29: note: suggested alternative:
In file included from /usr/include/c++/5/string:51:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from /home/uros/Namizje/CodeblocksProjects/exercise/main.cpp:1:
/usr/include/c++/5/bits/range_access.h:105:37: note:   ‘std::begin’
   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
                                     ^
/home/uros/Namizje/CodeblocksProjects/exercise/main.cpp:63:29: error: ‘end’ was not declared in this scope
     for(auto const &index : myarray)
                             ^
/home/uros/Namizje/CodeblocksProjects/exercise/main.cpp:63:29: note: suggested alternative:
In file included from /usr/include/c++/5/string:51:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from /home/uros/Namizje/CodeblocksProjects/exercise/main.cpp:1:
/usr/include/c++/5/bits/range_access.h:107:37: note:   ‘std::end’
   template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
                                     ^
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 

Title: Re: For each loop
Post by: oBFusCATed on November 07, 2017, 10:25:01 am
This is programming question, so a forum rules violation.

The problem is in your code for T *a, the compiler doesn't know the size of the array, so there is no way to implement correct begin and end. In VC you've probably done T a[10]...

You can check your code against different compilers here: https://godbolt.org/
Title: Re: For each loop
Post by: spartan on November 07, 2017, 10:49:21 am
Sorry, moderators can delete this thread if neccessary, I copied this code from tutorial and seemed correct to me that's why I posted it here.