4

I am new to code::blocks and quite unexperienced in c++ as well. I know there are many keywords, that I am not supposed to use for variable names and they are usually displayed in a different color, as they are in visualstudio. Now when I was copying some code fragments over from my old vs project to a new code::blocks project, some of my variable names suddenly got colored. For example in this code the variables begin and end are displayed in green now, and I don't understand why.

int begin = 0;
int end = 4;
int myarray[end];
for (int i = begin; i < end; i++)
{
    myarray[i] = i;
}

In some cases, this code won't even compile and I get strange "conflicting declaration" errors. May I use these names anyway, or are they somehow reserved in code::blocks? I looked into some books, but as far as I know, they are no c++ keywords. Is the reason, that I am using c++11 now? I am using v.12.11 of the ide and the mingw compiler, that comes with it. Thank you for your help.

Karl Franz
  • 43
  • 4
  • 5
    If you are using C++11 and `using namespace std;`, begin and end are actually functions. http://en.cppreference.com/w/cpp/iterator/begin – risingDarkness Aug 27 '13 at 13:54
  • I would recommend avoiding all generic variable names like begin, end, index, etc. A more descriptive name would make the code clearer and you completely avoid this whole issue. – Jerry Gagnon Aug 27 '13 at 13:54
  • Those aren't keywords. They are names of member functions of stantard library containers, and also of non-member functions in the `std::` namespace since C++11. Your IDE must be trained to recognise them. You should avoid those names, but using them is not necessarily an error. – juanchopanza Aug 27 '13 at 13:55
  • Go to Settings->Editor->Syntax Highlighter->Keywords... – chris Aug 27 '13 at 13:56
  • 5
    @JerryGagnon how can it be more descriptive? `firstIndexIWantToLoopOver` and `onePastTheLastIndexIWantToLoopOver`? – R. Martinho Fernandes Aug 27 '13 at 14:20
  • The beginning of what? The ending of what? The example given is a poor one because they are no better than constants or literals. In any event it is just a recommendation, take it or leave it. – Jerry Gagnon Aug 27 '13 at 14:33

2 Answers2

7

I take from your confusion, that you've not yet worked with STL containers... If you're new to C++ however, that might be a good idea. Anyway, begin and end are no keywords in any C++ standard, neither in C++11. But they are both names of functions, returning an iterator object, that is used to walk through an STL container like this:

vector<int> x = { 1, 2, 3, 4 };
vector<int>::iterator it;

for (it = x.begin(); it != x.end(); ++it)
{
    cout << *it << endl;
}

In C++ everyday practice this concept is so commonly used, that these names got listed as "user keywords" by CodeBlocks. User keywords are usually coloured green and thus distinguishable from language keywords. If that bothers you, you are free to manipulate the list or even erase it completely. Just choose "Settings" --> "Editor" from the menu bar and then click on the "Syntax highlighting" tab. There you can do all the settings you like. Get the manual for further information.

image http://imageshack.us/a/img189/3956/m1qe.png

No matter with or without syntax highlighting, you should't however get compilation errors. The reason may be, that your project includes standard library headers like #include <vector> and your code contains using namespace std; somewhere. You possibly work with a precompiled header - also check for it in this case.

Besides I would recommend you to have a look on standard library containers and give it a try. They have several advantages over plain arrays, but you better find out yourself. However, don't let yourself be intimidated by these iterators - in C++11 you could also write

vector<string> y = { "Foo", "Bar" };
for (auto& str : y) cout << str << endl;

instead, and that would also work with your arrays

float z[] = { 0.5f, 1.5f, 2.5f, 3.5f };
for (auto& num : z) cout << num << endl;
Rene R.
  • 534
  • 1
  • 4
  • 12
2

The variables begin and end are displayed in green now, and I don't understand why

You probably have a using namespace std; somewhere, dumping some or all of the standard library's names into an unsuitable scope. The library contains (among many, many other names) functions called begin and end, and your syntax highlighter is probably finding those.

In some cases, this code won't even compile

Sometimes, your names will hide the library names, and the code will compile. Sometimes, they won't, so you'll get an error.

May I use these names anyway, or are they somehow reserved?

Yes. Get rid of the inappropriate using directive(s), and you're free to use any non-reserved name wherever you like. The only reserved names are keywords, names with particular patterns of underscores and capital letters, and names in the std namespace.

Is the reason, that I am using c++11 now?

Yes. These functions were added to the standard library in 2011.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644