我有几个std::unordered_maps
。他们都有一个std::string
作为他们的关键和他们的数据不同。我想从给定的映射关键字创建一个csv字符串,因为这些数据需要通过线路发送到连接的客户端。目前,我对每个单独的地图都有一个方法。我想使这个通用的,我想出了以下内容:C++ 14在方法的定义中使用auto关键字
std::string myClass::getCollection(auto& myMap) {
std::vector <std::string> tmpVec;
for (auto& elem : myMap) {
tmpVec.push_back(elem.first);
}
std::stringstream ss;
for (auto& elem : tmpVec) {
ss << elem <<',';
}
std::string result=ss.str();
result.pop_back(); //remove the last ','
return result;
}
我用gcc 6.1.0和-std = C++编译14使用Eclipse和它编译,但它不会链接。 链接器抱怨未定义参考std::__cxx11::getCollection(someMap);
无论地图数据的,我把它的方式,它总是告诉我:
Invalid arguments ' Candidates are: std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>> getCollection() '
如何解决这个问题?
'std :: string myClass :: getCollection(auto&myMap)'是无效的语法。具体而言,'auto'不是成员函数的有效参数类型。 – ildjarn
是我试图完成可能使用另一种方法呢?我认为这是可能的使用自动作为参数在C++ 14 ...显然我错了,然后... – ZoOl007
“*我认为有可能使用自动作为参数在C++ 14 ... *“仅限于lambda。 “*是我试图用另一种方法完成的可能性吗?*“是的,只需使用普通模板:'template std :: string myClass :: getCollection(MapT&myMap)' –
ildjarn