简体   繁体   中英

C++ template with map allocator problem

I define a template function which loads a map from a CSV file:

template <class T>
bool loadCSV (QString filename, map<T,int> &mapping){
    // function here
}

I then try to use it:

map<int, int> bw;
loadCSV<int>((const QString)"mycsv.csv",&bw);

But get htis compile time error:

error: no matching function for call to 
‘loadCSV(const QString, std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >*)’

It seems my function call is bringing in some implicit arguments, but I don't understand the error and how to fix it. Any ideas?

Drop the ampersand, you don't want to pass a pointer to the map (notice the asterisk at the end of the error message). Also, you don't have to explicitly cast the string literal. Moreover, the compiler should be able to deduce the template argument automatically.

loadCSV("mycsv.csv", bw);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM