random_shuffle compilation problem

Aug 1, 2013 at 12:56am
When I first started C++, I used Dev-C++ 5.4.2. Recently I have tried out Code::Blocks 12.11 using the "GNU CCC Compiler", and I like it more, except for the following issue:

The following code compiles fine, and with no run-time errors, in Dev-C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <algorithm>
#include <iterator>

int main()
{
    std::srand(time(0));
    std::string colors[] = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "White", "Gray", "Black", "Magenta", "Cyan", "Brown"};

    std::random_shuffle( begin(colors), end(colors) ) ;

    const int NUMBER_OF_COLORS = 6 ;
    for ( int i = 0; i < NUMBER_OF_COLORS; ++i )
    {
        std::cout << "  " << colors[i] << " Worm" << '\n' ;
    }
}

However, the same code will not compile in Code::Blocks.
The error output message is:
Line 13: error: 'begin' was not declared in this scope
Line 13: error: 'end' was not declared in this scope


Why won't it compile on Code::Blocks? Edit: Sorry if I should've just posted this in CodeBlocks forum, I just thought about that.

Edit 2: I went to Build Options... -> Have g++ follow 1998 ISO C++ language standard, thinking that would fix it, but same compiler issue.
Last edited on Aug 1, 2013 at 1:05am
Aug 1, 2013 at 1:07am
begin/end on arrays are a C++11 only feature. Following the 1998 iso standard will not work.

I have never used codeblocks so I can't be too much more help, but see if there is an option for C++11 in the build options. Make sure you have the latest version of the compiler as well.
Aug 1, 2013 at 1:20am
Oh thanks, I thought just having 1998 would be fine due to the C++98 tab on http://www.cplusplus.com/reference/algorithm/random_shuffle/

Yep, it has 2011 and I am pretty sure the compiler is up-to-date, I downloaded it via this link if I remember correctly: http://sourceforge.net/projects/mingw/files/latest/download?source=files

I did this in the settings:
http://i1093.photobucket.com/albums/i434/GanadoFO/CodeBlocks_ISO2011.png
And it compiled correctly.

Thanks a lot!
Last edited on Aug 1, 2013 at 1:21am
Aug 1, 2013 at 6:29am
std::begin and std::end are in the std namespace.
Aug 1, 2013 at 9:13am
cire wrote:
std::begin and std::end are in the std namespace.

I think you don't need std:: in front of begin and end because of argument-dependent lookup. At least the code compiles for me with GCC 4.7.1.
Aug 1, 2013 at 6:26pm
I think you don't need std:: in front of begin and end because of argument-dependent lookup. At least the code compiles for me with GCC 4.7.1.


You are correct. It caught me off guard because colors is an array.

If the element type of colors is changed to int, it won't work -- might be that a using directive would be in order here in case the type is changed.
Topic archived. No new replies allowed.