Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does code::blocks reserve names other than c++ keywords?

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.

like image 704
Karl Franz Avatar asked Jan 13 '23 13:01

Karl Franz


1 Answers

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;
like image 182
Rene R. Avatar answered Jan 16 '23 00:01

Rene R.