I have several std::unordered_maps
. They all have an std::string
as their key and their data differs. I want to make a csv string from a given map's keys because that data needs to be sent over the wire to a connected client. At the moment I have a method for each individual map. I wanted to make this generic and I came up with the following :
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;
}
I compile with gcc 6.1.0 and -std=c++14 using eclipse and it compiles but it doesn't link.
The linker complains about undefined reference to std::__cxx11::getCollection(someMap);
Regardless of the map data and the way I call it, it always tells me :
Invalid arguments ' Candidates are: std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>> getCollection() '
How do I solve this?
std::string myClass::getCollection(auto& myMap)
is not valid syntax. Specifically,auto
is not a valid parameter type for a member function. – Dekkertemplate<typename MapT> std::string myClass::getCollection(MapT& myMap)
– Dekkerauto
in function parameters is currently non-standard, but might get into the language in C++20. It's called an "abbreviated function template" and I think it's part of the Concepts proposal. GCC provides it as an extension currently; if you compile with-pedantic
it'll fail. – Legume