代码::块保留名称而不是c ++关键字? [英] does code::blocks reserve names other than c++ keywords?

查看:132
本文介绍了代码::块保留名称而不是c ++关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的代码::块和相当不熟悉在c ++以及。我知道有很多关键字,我不应该用于变量名称,他们通常显示不同的颜色,因为他们在visualstudio中。现在,当我把一些代码片段从我的旧vs项目复制到一个新的代码:: blocks项目,我的一些变量名突然变得颜色。例如在这段代码中,变量begin和end现在以绿色显示,我不明白为什么。

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

在某些情况下,这些代码甚至不会编译,冲突声明错误。我可以使用这些名称,无论如何,或者他们不知何故保留在代码::块?我看了一些书,但据我所知,他们没有c ++关键字。是原因,我现在使用c ++ 11?我使用的v.12.11的ide和mingw编译器,它自带。感谢您的帮助。

解决方案

我从你的困惑中解脱出来,你还没有使用STL容器...如果你是新来的C ++,但是,这可能是一个好主意。无论如何, begin end 在任何C ++标准中都不是关键字,在C ++ 11中都没有。但它们都是函数的名称,返回一个迭代器对象,用于遍历STL容器,如下所示:

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

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

在C ++的日常实践中,这个概念是如此常用,作为用户关键字由CodeBlocks。用户关键词通常被着色为绿色,因此可以与语言关键词区分开来。如果这麻烦你,你可以自由操纵的列表,甚至完全删除它。只需从菜单栏中选择设置 - >编辑器,然后单击语法高亮选项卡。在那里你可以做你喜欢的所有设置。有关详情,请参阅,并试试。他们有比普通数组有几个优点,但你最好找出自己。但是,不要让自己被这些迭代器吓倒 - 在C ++ 11中你也可以写

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

而且也可以使用你的数组

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


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.

解决方案

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.

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;

这篇关于代码::块保留名称而不是c ++关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆