Talk About Network

Google





Programming > C - C++ Learning > Re: C++ the STL...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 9 Topic 4225 of 4400
Post > Topic >>

Re: C++ the STL and std::map

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Jul 30, 2008 at 06:01 PM

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/
 




 9 Posts in Topic:
C++ the STL and std::map
Andrew Falanga <af300w  2008-07-29 14:46:21 
Re: C++ the STL and std::map
"Jim Langston"   2008-07-29 14:59:34 
Re: C++ the STL and std::map
Andrew Falanga <af300w  2008-07-29 15:16:08 
Re: C++ the STL and std::map
Ben Bacarisse <ben.use  2008-07-30 00:54:56 
Re: C++ the STL and std::map
Andrew Falanga <af300w  2008-07-30 07:22:31 
Re: C++ the STL and std::map
"Jim Langston"   2008-07-30 07:50:30 
Re: C++ the STL and std::map
Bart van Ingen Schenau &l  2008-07-30 18:01:16 
Re: C++ the STL and std::map
Andrew Falanga <af300w  2008-07-30 07:55:19 
Re: C++ the STL and std::map
"Jim Langston"   2008-07-30 08:01:22 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Thu Jan 8 12:59:49 PST 2009.