質問

I have the following vector

std::vector< std::pair<std::string, std::pair<std::string, std::string>  > > vecList;

In the loop I add the entry:

vecList.push_back(std::make_pair(name.toStdString(), std::make_pair(path.toStdString(), arguments.toStdString())));

I want to call:

std::sort(vecList.begin(), vecList.end(), CompareSort);


bool SettingsData::CompareSort(const std::pair <std::string, std::pair<std::string, std::string> > &first,
                               const std::pair< std::string, std::pair<std::string, std::string> > &second)
{
    unsigned int i=0;

    /*
    while ( (i < first.length()) && (i  <second.length()) )
    {
      if (tolower(first[i]) < tolower(second[i]))
          return true;
      else if (tolower(first[i]) > tolower(second[i]))
          return false;
      ++i;
    }
    return ( first.length() < second.length() );
    */

    return true;
}

I want to sort by the first pair (name) string, but when I get a compile error:

error: no matching function for call to 'sort(std::vector<std::pair<std::basic_string<char>, std::pair<std::basic_string<char>, std::basic_string<char> > > >::iterator, std::vector<std::pair<std::basic_string<char>, std::pair<std::basic_string<char>, std::basic_string<char> > > >::iterator, <unresolved overloaded function type>)'
     std::sort(vecList.begin(), vecList.end(), CompareSort);

Can anyone give a tip on what I can be doing wrong

Updated Fix In my header file I just set the declaration to static:

static bool CompareSort(const std::pair< std::string, std::pair<std::string, std::string> > &first,
                 const std::pair< std::string, std::pair<std::string, std::string> > &second);

Thanks to Joachim suggestion

役に立ちましたか?

解決

The most likely reason is that CompareSort is a non-static member function, which means it actually have a hidden first argument (the this pointer). If you can make it static it should work okay. Otherwise you can make the function a non-member function, or use std::bind.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top