Does code :: block fallback name names other than C ++ keywords?

I am new to code :: blocks and completely inexperienced in C ++. I know that there are many keywords that I should not use for variable names, and they usually appear in a different color since they are in visualstudio. Now that I copied the code snippets from my old vs project to the new code :: blocks project, some of my variable names suddenly became colored. For example, in this code, the begin and end variables 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 does not even compile, and I get strange errors of a "conflict declaration". Can I use these names anyway or are they somehow reserved in code :: blocks? I looked through several books, but as far as I know, they are not C ++ keywords. Is the reason that I am using C ++ 11 now? I am using v.12.11 ide and the mingw compiler that comes with it. Thank you for your help.

+4
source share
2 answers

I understand that you have not worked with STL containers yet ... If you are new to C ++, this might be a good idea. In any case, begin and end are not keywords in either the C ++ standard or C ++ 11. But they are both function names, returning an iterator object that is used to navigate through the STL container as follows:

 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 used so often that these names were listed as “custom keywords” CodeBlocks. User keywords are usually green and therefore different from language keywords. If this bothers you, you can manipulate the list or even completely erase it. Just select "Settings" → "Editor" in the menu bar, and then click on the "Syntax Highlighting" tab. There you can make all the settings that you like. Get manual for more information.

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

Regardless of whether or not syntax highlighting, you should, however, get compilation errors. The reason may be that your project includes standard library headers, such as #include <vector> , and your code contains using namespace std; somewhere. Perhaps you are working with a precompiled header - also check it in this case.

In addition, I would recommend you take a look at the standard library containers and give it a try. They have several advantages over conventional arrays, but it is better to know yourself. However, do not let yourself be intimidated by these iterators - in C ++ 11 you can also write

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

and this will also work with your arrays

 float z[] = { 0.5f, 1.5f, 2.5f, 3.5f }; for (auto& num : z) cout << num << endl; 
+6
source

The start and end variables are shown in green and I don't understand why

You probably have using namespace std; somewhere, dropping some or all of the standard library names into an inappropriate area. The library contains (among many, many other names) functions called begin and end , and your syntax marker will probably find them.

In some cases, this code does not even compile

Sometimes your names will hide library names, and the code will be compiled. Sometimes they will not, so you get an error message.

Can I use these names anyway or are they somehow reserved?

Yes. Get rid of inappropriate using directives, and you can use any unreserved name wherever you want. The only reserved names are keywords, names with specific underscore and capitalization patterns, and names in the std .

Is the reason that I am using C ++ 11 now?

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

+2
source

Source: https://habr.com/ru/post/1499151/


All Articles