在boost网站上使用这个链接,我可以编译代码这里。甚至得到答案(例如)。但是,当我通过在类中定义来做同样的事情时,我得到的错误如下:
#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为了正确编译代码,我必须按照如下所示使用the和typename。但我仍然无法使用(插入或foo函数(如例所示)。
请帮助,这是使用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);
};我必须将这个关联属性映射传递给另一个类,在那里我可以像往常一样使用get和put函数。我该怎么处理呢?
发布于 2014-02-03 04:27:18
这是:
boost::const_associative_property_map< std::map<std::string, std::string> > address_map(name2address);声明一个变量并通过使用name2address调用构造函数来初始化它。不能在类声明中这样做,必须在类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) {}
};这应该可以解决编译问题,但我不确定它是否是最好的布局,取决于之后您将如何使用Test。
https://stackoverflow.com/questions/21519502
复制相似问题