提高:: associative_property_map()编译出错

问题描述:

在使用升压网站这个环节,我能够编译code的此处。甚至得到的答案(示例)。但是,当我做同样通过在一个类中定义,我得到的错误如下:

using this link on boost website, I am able to compile the code here. and even get the answer (example). But when I do the same by defining in a class, I get the error as follows:

#include < as usual in the code >

class Test{

 std::map<std::string, std::string> name2address;
 boost::const_associative_property_map< std::map<std::string, std::string> >
                  address_map(name2address);


};

误差如下:

error: ‘name2address’ is not a type

我有如下编译code为正确的typedef和类型名称使用。但我仍然无法使用(插入,或者作为例子中提供的功能美孚()

I have to use typedef and typename as follows to compile the code correctly. but I am still unable to use (insert, or the foo function (as provided in example)

请大家帮忙,这是使用类型定义了code

please help, here is the code using typedefs

class Test{
  typedef typename std::map<std::string, std::string> name2address;
  boost::const_associative_property_map< std::map<std::string, std::string> >
                address_map(name2address);

 };

我要这个关联属性映射传递给另一个班,在那里我可以用得到,并把功能如常。如何将我的做法呢?

I have to pass this associative property map to another class, where I can use get and put functions as usual. How would i approach that ?

boost::const_associative_property_map< std::map<std::string, std::string> > address_map(name2address);

声明一个变量,并通过调用name2address构造函数初始化。你不能这样做在一个类声明,你必须做在构造函数类:

declares a variable and initialize it by calling the constructor with name2address. You cannot do it in a class declaration you have to do it in the class ctor :

class Test{

 std::map<std::string, std::string> name2address;
 boost::const_associative_property_map< std::map<std::string, std::string> >
                  address_map;
public:
 Test() : address_map(name2address) {}
};

这应该可以解决编译问题,但我不知道这是最好的布局取决于您将如何后使用测试。

This should solve the compilation issue, but I'm not sure it is the best possible layout depending on how you will use Test after that.