Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
2.78/5 (3 votes)
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
Updated 11-Dec-13 1:52am
v2

You can't write "normal" code outside a function declaration. Move it inside the Main method:
C#
charMap g_charMap;
int main()
   {
   g_charMap.insert(make_pair('{','}'));
   //some stuff is there.
   }
 
Share this answer
 
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.
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 :)
 
Share this answer
 
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<char,char>
{
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 :)
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900