{"id":6504,"date":"2014-04-19T04:41:50","date_gmt":"2014-04-19T04:41:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/19\/how-to-declare-use-extern-map-in-c-collection-of-common-programming-errors\/"},"modified":"2014-04-19T04:41:50","modified_gmt":"2014-04-19T04:41:50","slug":"how-to-declare-use-extern-map-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/19\/how-to-declare-use-extern-map-in-c-collection-of-common-programming-errors\/","title":{"rendered":"How to declare\/use extern map in c++-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m not very used and don&#8217;t really understand the use of global variables in c++, and cannot manage to solve a problem here, although I&#8217;m nearly sure I&#8217;m using the declarations the right way. I&#8217;ve got an unordered map, which contains params from a file. This is working fine, I&#8217;ve outputted the contents, everything ok. But I would like this map to be available everywhere in my program, so that I don&#8217;t have to pass it as an argument every time, hence the use of global variable. I&#8217;ve a little typedef :<\/p>\n<pre><code>typedef tr1::unordered_map unmap;\n<\/code><\/pre>\n<p>then the function to read the parameters:<\/p>\n<pre><code>unmap readParameters(string filename);\n<\/code><\/pre>\n<p>then the global scope declaration (everything outside the main of course, although in the same file, I still don&#8217;t have any headers):<\/p>\n<pre><code>extern unmap params;\n<\/code><\/pre>\n<p>The affectation is made in the main() function:<\/p>\n<pre><code>unmap params=readParameters(inputfile_name);\n<\/code><\/pre>\n<p>(Note: I tried with and without the &#8220;unmap&#8221;, but same error). And I&#8217;ve got a function displayInformation() to display its contents in which I use my map &#8220;params&#8221;:<\/p>\n<pre><code>for (unmap::iterator it=params.begin();it!=params.end();it++){\n    cout<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m not very used and don&#8217;t really understand the use of global variables in c++, and cannot manage to solve a problem here, although I&#8217;m nearly sure I&#8217;m using the declarations the right way. I&#8217;ve got an unordered map, which contains params from a file. This is working fine, I&#8217;ve outputted the contents, everything ok. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6504","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6504"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6504\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}