可以在函数体外写出声明吗? [英] Can statements be written outside function body?
问题描述
您好,
作为C ++的初学者,我需要您的帮助来澄清基本的
概念。示例C ++代码如下:
int i; // A
i ++; // B,错误
int main()
{
i ++; // C
返回0;
}
虽然两者都是合法的C ++声明,// A& ; // B在这个
代码中有所不同。 // A是正确的但// B是错的。语句// A,// B
也是合法的,但// B不能在函数体外发生。移动
// B到位置// C后,错误警报消失了。请解释为什么。
谢谢。
你的真诚
Joe Li
Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can''t occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
Thank you.
Your Sincerely
Joe Li
推荐答案
Jian H. Li写道:
Jian H. Li wrote:
你好,
作为C ++的初学者,我需要你的帮助澄清基本的概念。示例C ++代码如下:
int i; // A
i ++; // B,错误
int main()
{+ / i ++; // C
返回0;
}
虽然两者都是合法的C ++声明,// A& // B在此代码中有所不同。 // A是正确的但// B是错的。语句// A,// B
同样合法,但// B不能在函数体外发生。将
// B移动到位置// C后,错误警报消失了。请解释为什么。
Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can''t occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
您可以在全局范围内声明变量(在任何函数或
类主体之外),但必须放置语句这不是声明进入
函数体。这就是C和C ++的设计方式。
You can declare a variable in the global scope (outside any function or
class body), but must put statements that are not declarations into a
function body. That''s how C and C++ have been designed.
" Jian H. Li" <乔**** @ xinhuanet.com>在消息中写道
news:c9 ************************** @ posting.google.c om ...
"Jian H. Li" <jo****@xinhuanet.com> wrote in message
news:c9**************************@posting.google.c om...
你好,
作为C ++的初学者,我需要你的帮助来澄清基本的概念。示例C ++代码如下:
int i; // A
i ++; // B,错误
int main()
{+ / i ++; // C
返回0;
}
虽然两者都是合法的C ++声明,// A& // B在此代码中有所不同。 // A是正确的但// B是错的。语句// A,// B
同样合法,但// B不能在函数体外发生。将
// B移动到位置// C后,错误警报消失了。请解释为什么。
谢谢。
你诚挚的李小姐
Hello,
As a beginner to C++, I need Your kind help to clarify the basic
concept. The sample C++ code as following:
int i; // A
i++; // B, error
int main()
{
i++; // C
return 0;
}
Though both are legal C++ statement, //A & //B are different in this
code. //A is correct but //B is wrong. The same as statement //A, //B
is legal also, but //B can''t occur outside function body. After moving
//B to position //C, the error alert disapeared. PLEASE EXPLAIN WHY.
Thank you.
Your Sincerely
Joe Li
因为你可以不要在函数体外部放置语句。
如果可以,那么你认为这些语句什么时候应该执行?
john
Because you can''t put statements outside a function body.
If you could then when do you think the statements should be executed?
john
2004年2月11日星期三12:12:24 -0000 in comp.lang.c ++,John Harrison
< jo *************@hotmail.com>据称写了:
On Wed, 11 Feb 2004 12:12:24 -0000 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> was alleged to have written:
如果你能在什么时候认为这些陈述应该被执行?
If you could then when do you think the statements should be executed?
嗯,显然它应该同时执行,如果你写了
:
int i;
int j = i ++;
Well, obviously it should be executed at the same time it would be if
you had written:
int i;
int j = i++;
这篇关于可以在函数体外写出声明吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!