Click here to Skip to main content
12,065,756 members (26,597 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
#include<map>
using namespace std;
typedef map<char,char> charMap;
 
//take a global variable for this map
charMap g_charMap;
int i; //it is working fine
g_charMap.insert(make_pair('{','}'));//Above mentioned Error is shown in this line

int main()
{
  //some stuff is there.
}

Can anyone please tell me reason and solution?
Posted 11-Dec-13 2:38am
Edited 11-Dec-13 2:52am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can't write "normal" code outside a function declaration. Move it inside the Main method:
charMap g_charMap;
int main()
   {
   g_charMap.insert(make_pair('{','}'));
   //some stuff is there.
   }
  Permalink  
Comments
Rahul@puna 11-Dec-13 7:50am
   
But if I want to use that global map in some other funtion then how can we access the map if we declare that in main function.
And why cant we write normal code outside a function.
OriginalGriff 11-Dec-13 8:13am
   
You aren't declaring it in the Main function; the declaration is outside, making it a global variable.
You are setting it's value in the Main function - the variable will retain that value from that point on.
Since Main is the first function executed, the value is then set for the lifetime of the application.
Rahul@puna 11-Dec-13 8:17am
   
I am Ok with ur explanation.
But my 2nd question is still remain unanswered i. why cant we put normal code outside the funtion.
OriginalGriff 11-Dec-13 8:28am
   
:laugh:
When would it execute it? In what order? "Nearest to the top of the file first"? What if there are two or more files in the project?
What if it relies on some other line of code? Has that been executed yet?

Should it be run before Main, or after?

Execution starts with the first instruction in Main, and continues from there until Main exits (this is true in broad terms fall all applications, be they console, windows, or web) - so there isn't really any time when it is "safe" to execute it!
Rahul@puna 11-Dec-13 8:39am
   
Means u r saying that it will results in unexpected behaviour(unsafe) that is why compiler is giving error.
I don't know whether this explanation is right or not.
By the way I have some doubt that is why I have put this question. Sorry, It is not a matter of laughing. Ur knowledge is of no use unless u r able to make it clear to someone.
May be I am not able to get it properly.
OriginalGriff 11-Dec-13 9:29am
   
Please don't take laughter as a personal slight: it's no meant that way, I used it to indicate that I missed your second question, and to indicate that it's not a "real" question (there is no single "right" answer as to why you can't do it)

It's not so much that it "will result in unexpected behaviour" - there are lots of things that will do that - but that the language specification will not allow it because there are no rules for when it would be executed. Bear in mind that C++ derives from C, which also didn't allow code outside functions, and the designers of C++ saw no good reason to allow it (quite rightly IMHO). The original design of C didn't allow it probably because in part it would have complicated the compiler beyond the limits of the equipment it was to run on!

Asking "Why" questions with computer languages often boil down to "Because. Just 'because'." because they are generally designed by committees and they only release the specification, not the arguments and reasoning behind it.
Rahul@puna 11-Dec-13 11:43am
   
sry for misunderstanding.
Thanks for ur explanation.
OriginalGriff 11-Dec-13 11:54am
   
You're welcome!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

A global object does provide its construcor's and destructor's calls only, in the "global scope".
Try to place the call into your main function Smile | :)
  Permalink  
Comments
Rahul@puna 11-Dec-13 7:53am
   
can u elaborate ur answer?
Eugen Podsypalnikov 11-Dec-13 7:57am
   
It would be a "global" insert, before the object's usage in any functions :) :
class charMap : public std::map
{
public:
charMap() {
insert(make_pair('{','}'));
}
} g_map;
Rahul@puna 11-Dec-13 8:12am
   
Its Ok then.
Why the problem occured I m still not clear.
Eugen Podsypalnikov 11-Dec-13 8:17am
   
Because of the placement of std::map::insert(..)
The call can be in a function scope only.
The declaration of the map object may be global :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Search for singleton pattern on the web...

Essentially instead of using a global variable, you use a static function Inside a class that return the unique instance of the object which is typically created on the first use.

In multithreaded program locks can also be used for synchronization or you might ensure that the instance is created before any additionnal thread.

Alternatively embedding the map Inside another object with a constructor (or deriving from the map) would allows you to fill that static object at construction time from container object constructor (or derived class constructor if you opt for that option).

Remember that you should always avoid global variables. Singleton is a good compromise in many cases.

Finally, there is alwaus the simple solution to fill the map from the main. In general, you also want to avoid complex code to be executed before main as it make harder to properly handle errors that might occurs at this stage.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Above solution is good. To know more about storage classes I found this resource over google Storage Classes
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160207.1 | Last Updated 25 Dec 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100