Initialize a std::unordered_map object in a constructor initialization list

As the title says, I'm having trouble figuring out how to initialize an unordered_map object in my class constructor. Here are the very beginnings of the class I'm trying to make:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include <unordered_map>
#include <string>
#include <utility>

using namespace std;
typedef pair<double, double> stats;

class BuildInfo
{
private:
	unordered_map<string, string> info;
	const double BUILD_GROWTH = 1.15;

public:
	BuildInfo() : info ({{"Cursor", "test"}, {"Grandma", "test"}, 
		{"Farm", "test"}, {"Factory", "test"}})
	{
	}
};


Intellisense complains that the first bracket "expected an expression" and the first and last closing brackets "expected a ';'". According to the reference on this site, unordered_map objects can be initialized like so:

1
2
3
    typedef std::unordered_map<std::string,std::string> stringmap;
    //snipped unnecessary code
    stringmap third ( {{"orange","orange"},{"strawberry","red"}} );

I can't figure out how this is different from what I'm doing. Sorry if this is a dumb question, but I've been going back and forth between like 10 different browser tabs and I can't seem to figure it out.

Also, while I'm here, is it possible to use an anonymous object as an argument to a function? For example, in Java you can do things like
 
window.setLayout(new FlowLayout(FlowLayout.LEFT));

which will create the specified FlowLayout object and immediately pass it off to setLayout. What I want to do is use anonymous stats objects instead of the "test" strings to initialize the map items in my code above. Can I do that and if so, how?
Intellisense errors are not compile errors. The code is fine:
http://ideone.com/WEeiyJ
Oh, oops. Thanks for the response. Any chance you could explain your comments on my code? Specifically, what's wrong with precompiled headers (I thought they made your project compile faster), and is it appropriate to use "using namespace x;" with a narrower scope, or should I just avoid it altogether?

Also, does anybody have an answer to the anonymous object question?

edit: just tried to compile the code in visual studio (exactly as posted), and it gave me a bunch of errors. The project is basically nothing except an empty main method in one file and the stuff above in another. The errors are all syntax errors relating to the constructor initialization statement
Last edited on
Precompiled headers don't work very well. Do some Googling.

using namespace std; -> http://stackoverflow.com/q/1452721/1959975 (via Google)

As for your anonymous object question: the syntax is the same as in Java except that you do not use the keyword new. The function must accept the parameter by copy or by const-reference, the temporary object cannot bind to a non-const reference.
Last edited on
Thanks again! I've saved that page for future reference, lots of helpful info :). Unfortunately, VS 2012 won't compile that class (see edit above)! Why would it work for you but not for me?
Which version of Visual Studio are you using? The latest I have is 2013. Visual Studio has been slow to adopt the C++11 standard - later versions support more of the standard. Specifically in your code you are using C++11 braced initialization.
It's the latest update of the 2012 version, which is what we're supposed to have for the Visual Basic class I'm taking right now. I do have access to 2013 through my school as well; I guess I'll try upgrading and see what happens. What would that statement look like in pre-C++11 syntax (everything I've found seems to use the brace syntax)?
Visual Studio 2013 Express Edition is free.

There's not a way to initialize the map properly before C++11. You have to add the elements in the body of the constructor after the map has been initialized via default constructor.

Alternatively you could store a static map instance and copy it to construct your map.
1
2
3
4
5
6
7
8
9
10
private:
    static unordered_map<string, string> const &default_map()
    {
        static unordered_map<string, string> def;
        if(def.size() == 0)
        {
            def["blah"] = "meh";
        }
        return def;
    }
BuildInfo() : info(default_map()) {}
Last edited on
Yeah, those both seem...suboptimal. Looks like braces are the way to go. I'm downloading VS 2013 now, so hopefully once I've got that up and running all will be well. Thanks so much for all your help!
Topic archived. No new replies allowed.