Andrew Falanga wrote:
>>
>> Just to be sure (it won't be so simple) when you say "as outlined
>> above" you don't really have assignments outside of a function body
>> do you? You say it works in gcc, so it can't be that, but please
>> post the real code (cut down if need be) rather than an outline.
>>
>> --
>> Ben.
>
>
> Ben,
>
> You are to be commended for you analytical skill. I need to be
> educated. Why is this a bad thing? (And yes, this is what I'm
> doing) Basically, I have this in my *.cpp file:
>
> #include <thecodes.h>
> #include <map>
>
> std::map <unsigned short int, const char*, std::less<unsigned short
> int> > mCodeMap;
This line in itself is not a *bad thing*. (unless you consider global
variables to be bad, but the C++ standard doesn't.)
>
> mCodeMap[SOME_MACRO] = SOME_MACRO_STR;
This line *is* a *bad thing*.
This is an assignment statement. As such, it it not a declaration and
only declarations (and definitions) are allowed outside of a block.
Code that must be executed at runtime can only appear in a function.
A declaration like
std::string foo("This is an example");
causes some code to be executed, but the declaration itself is not an
executable statement.
>
> // .......
>
>
> I need to understand why this is a bad thing because I've done things
> like:
>
> some.cpp
>
> #include <iostream>
>
> const int someInt = 0; // this is an assignment too outside of a
> function
This is *not* an assignment. Here, the =-sign is a punctuator to
indicate that the variable 'someInt' must be initialised with the value
0.
You can tell the difference by the fact that there is a type specified
before the variable name.
>
> And the code has compiled. Why is this not working because the
> assignments are outside of a function body?
>
> Frankly, I knew the problem had to be this simple because the code
> matched my skeleton program that I wrote in Linux to test against
> gcc. I knew there had to be a very subtle difference, and this has to
> be it. So, again, why is this a problem?
>
> Andy
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/


|