У меня возникла проблема в упорядочивании набора строк пользовательским компаратором. Я заказываю в соответствии с длиной строки.
struct CompareLenght
{
bool operator()(const std::string s1, const std::string s2) const
{return (s1.size()>s2.size());}
}
class SearchEngine
{
private:
void Associate(const std::vector<std::string> &Files);
public:
SearchEngine(const std::vector<std::string> &Files);
void Search(const std::string &frase, std::set<std::string> *S);
};
class FindWord
{
private:
std::set<std::string> Parole_Univoche;
public:
FindWord(const std::string &str);
inline void Get_Word(std::set<std::string> *Set_Of_Word);
};
inline void FindWord::Get_Word(std::set<std::string> *Set_Of_Word)
{
*Set_Of_Word = Parole_Univoche;
}
Проблема возникает, когда я пытаюсь это сделать:
void SearchEngine::Search(const std::string &phrase, std::set<std::string> *S)
{
FindWord F(phrase);
std::set<std::string,CompareLenght> Keyword;
F.Get_Word(&Keyword);
// some code
}
Ошибка:
In file included from main.cc:15:0:
SearchEngineNew.cc: In member function ‘void SearchEngine::Search(const string&, std::set<std::basic_string<char> >*):
SearchEngineNew.cc:69:33: error: no matching function for call to ‘FindWord::Get_Word(std::set<std::basic_string<char>, cmp>*)
F.Get_Word(&Keyword);
^
SearchEngineNew.cc:69:33: note: candidate is:
In file included from main.cc:14:0:
FindWord.cc:39:13: note: void FindWord::Get_Word(std::set<std::basic_string<char> >*)
inline void FindWord::Get_Word(std::set<std::string> *Set_Of_Word)
^
FindWord.cc:39:13: note: no known conversion for argument 1 from ‘std::set<std::basic_string<char>, cmp>* to ‘std::set<std::basic_string<char> >*
Как решить эту проблему? Большое спасибо!
Вы определили как
std::set<std::string,CompareLenght> Keyword;
это тип std::set<std::string,CompareLenght>
слова std::set<std::string,CompareLenght>
Но функция Get_Word имеет параметр типа std::set<std::string> *
inline void FindWord::Get_Word(std::set<std::string> *Set_Of_Word)
{ *Set_Of_Word = Parole_Univoche;}
Измените его следующим образом
inline void FindWord::Get_Word(std::set<std::string, CompareLenght> *Set_Of_Word)
{ *Set_Of_Word = Parole_Univoche;}
Также представляется, что функция должна быть определена как
inline void FindWord::Get_Word(std::set<std::string, CompareLenght> *Set_Of_Word)
{ Parole_Univoche = *Set_Of_Word; }
потому что в задании нет никакого смысла
*Set_Of_Word = Parole_Univoche;
Также член данных
std::set<std::string> Parole_Univoche;
следует также определить как
std::set<std::string, CompareLenght> Parole_Univoche;
Вы передаете & KeyWord, который имеет тип:
std::set<std::string,CompareLenght> Keyword;
к методу с параметром типа:
std::set<std::string>*
разница в этих типах - отсутствие CompareLenght в типе параметра Get_Word. Вы можете сделать этот mathod методом шаблона, чтобы принять любой тип KeyWord или добавить перегруженный метод. Или просто измените Get_Word на:
inline void Get_Word(std::set<std::string,CompareLenght> *Set_Of_Word);
^^^^^^^^^^^^^
но вам придется затем распространять это изменение на свой Parole_Univoche или копировать Set_Of_Word вручную так:
typedef std::set<std::string> set_type;
for ( set_type::const_iterator citr = Parole_Univoche.begin(); citr != Parole_Univoche.end(); ++citr ) {
Set_Of_Word->insert(*citr);
}
CompareLenght
(помимо наличия опечатки) принимает свой вклад по значению, что является ненужным и неэффективным. Скорее вы должны изменить сигнатуру компаратора наbool operator()(const std::string& s1, const std::string& s2)
.