boost::associative_property_map() compile error

寵の児 提交于 2019-12-02 17:45:35

问题


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 is as follows:

error: ‘name2address’ is not a type

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)

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 ?


回答1:


This:

boost::const_associative_property_map< std::map<std::string, std::string> > address_map(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.



来源:https://stackoverflow.com/questions/21519502/boostassociative-property-map-compile-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!