<ypjofficial@indiatimes.com> wrote in message
news:1118730073.081333.183570@o13g2000cwo.googlegr oups.com...[color=blue]
> Pls look at this code
> ////////////////////start///////////
> class a
> {
> public:
> a(){
> cout<<"inside constructor of a"<<endl;
> }
>
> int b()
> {
> int i=90;
> return i;
> }
> };
>
> a aa;//creating global object of a;
> //confusion 1
> aa.b();//this will give an compile error
>
> int i = aa.b();//but this will work fine.
>
> void main()
> {
> }
> //////////code ended/////////////
>
> The confusion is
> after i create a global object, i can't call globally any of its
> function in the standalone expression.Like aa.b();
> but the next statement will work fine.(int i = aa.b();)
>
> Why the compiler is behaving like this?
> What is the rule here which the compiler is following?[/color]
ISO/IEC 14992, section 3.5, paragraph 1:
A program consists of one or more translation units
linked together. A translation unit consists of a sequence of
declarations.
"aa.b();" is not a declaration, so it's illegal at global
scope.
To put it simply, you can't write "global code" in C++.
Code must be inside functions.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----