一个c ++基本问题 [英] a c++ basic question

查看:63
本文介绍了一个c ++基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的程序,我不认为'i'是重新定义的,但是有一个错误说:E:\ c ++ test\test\a.cpp(5):错误C2374: '我':重新定义;多次初始化

E:\ c ++ test\test\a.cpp(4):看到'i'的声明

有人能帮帮我吗?告诉我为什么?



Below is my procedure ,I don't think the 'i' is redefine,but there is an error says:E:\c++test\test\a.cpp(5) : error C2374: 'i' : redefinition; multiple initialization
E:\c++test\test\a.cpp(4) : see declaration of 'i'
can anybody help me? tell me why?

#include <iostream>
void main()
{
	for(int i=0;i<10;i++);
	for(int i=0;i<10;i++);

}

推荐答案

这是MS编译器的一个古老而已知的问题,即for循环声明变量在函数的其余部分具有范围。



在纯C中它是错误的 - 但它是或者它是MS标准。



AFAIK你在旧的VC 6.0中没有选择它。较新版本的VS真的更好。我个人最喜欢2008年。您可以免费下载快递版



首先声明int for循环或使用不同的名称。



解决方法是

it is an old and known problem of the MS compilers, that in for loops declared variables have scope in the remaining part of the function.

In "pure C" it is wrong - but it is or it was "MS Standard".

AFAIK you havent the option to it in the good old VC 6.0. Newer version of VS are really better. I personally liked the 2008 most. You can download the Express Version for free.

Declare the int above all for loops or use different names.

A workaround would be
void main()
{
   for(int i=0;i<10;i++);
    //open a deeper scope	
   {
      for(int i=0;i<10;i++);
   }
}


Quote:

我不认为'i'是重新定义的

I don't think the 'i' is redefine

确实,你是对的。



您的编译器是旧的还是不兼容的(或者两者兼有: - ))。我建议你得到一个更新的编译器。



C ++ 中,变量在 for 语句在循环本身中是正确的范围,因此没有重新定义,代码中没有双重初始化。



此外,标准 C ++ 不允许 void man():主函数必须返回一个 int

Indeed, you are right.

Your compiler is either old or not compliant (or both :-) ). I suggest you getting a more up-to-date compiler.

In C++, variables declared in the for statement are properly scoped in the loop itself, hence no redefinition , no double initialization in your code.

Moreover, standard C++ doesn't allow void man(): the main function must return an int.


取决于编译器:有些比其他编译器更灵活。

对于某些编译器这是重新定义,因为他们认为 i 的范围仅限于整个主函数,而不仅仅是 与之关联的循环。



但是,你得到它的最可能的原因是在第一个循环结束时忘记分号,这将是make it:

Depends on the compiler: some are more flexible than others.
For some compilers this is a redefinition because they see the i as having a scope limited to the whole main function, rather than just the for loop with which it is associated.

However, the most likely reason you are getting it would be to forget the semicolon at the end of the first loop, which would make it:
#include <iostream>
void main()
{
    for(int i=0;i<10;i++)
        for(int i=0;i<10;i++);
}

除非您直接从源文件中复制'n'pasted代码,否则我会查看...



但即使你这样做,在循环结束时使用分号是一种非常非常糟糕的做法,如果你的编译器没有给你一个警告,那么你需要启用它。如果它给你一个警告,那么你需要将它的选项更改为将警告视为错误 - 尤其是在你学习的时候。

编写这样的代码太容易了:

Unless you copy'n'pasted your code direct from your source file, I'd have a look for that...

But even if you did, it is a very, very bad practice to use a semicolon at the end of a loop, and if your compiler is not giving you a warning then you need to enable it. If it is giving you a warning, then you need to change it's options to "Treat warnings as errors" - particularly when you are learning.
It's far too easy to write code like this:

#include <iostream>
void main()
   {
   int i;
   for(i=0;i<10;i++);
      {
      printf(i);
      } 
   }

并且花了很长时间试图解决你的程序出了什么问题。

还有其他类似你需要的小陷阱确保teh compielr赢了;让你做:

And spend a long time trying to work out what is wrong with your program.
There are other little traps like that you need to make sure teh compielr won;t let you make:

if (a == b);
   c = 0;

经典:

And the classic:

if (a = b)
   c = 0;

使编译器成为尽可能严格防止大量的头发拉动!

Making your compiler as strict as possible prevents a lot of hair pulling!


这篇关于一个c ++基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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