Question

I have a problem in ordering a set of string by custom comparator. I would order according to string length.

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;
}

The problem comes when I try to do that:

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
}

The error is:

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> >*’

How can solve this problem? Thank you very much!

Was it helpful?

Solution 2

You defined set as

std::set<std::string,CompareLenght> Keyword;

that is the type of Keyword is std::set<std::string,CompareLenght> But function Get_Word has parameter of type std::set<std::string> *

inline void FindWord::Get_Word(std::set<std::string> *Set_Of_Word)
           {  *Set_Of_Word = Parole_Univoche;} 

Change it the following way

inline void FindWord::Get_Word(std::set<std::string, CompareLenght> *Set_Of_Word)
           {  *Set_Of_Word = Parole_Univoche;} 

Also it seems the function should be defined as

inline void FindWord::Get_Word(std::set<std::string, CompareLenght> *Set_Of_Word)
           {  Parole_Univoche = *Set_Of_Word; }

because there is no any sense in the assignment

*Set_Of_Word = Parole_Univoche;

Also data member

std::set<std::string> Parole_Univoche;

should be also defined as

std::set<std::string, CompareLenght> Parole_Univoche;

OTHER TIPS

You are passing &KeyWord which is of type:

std::set<std::string,CompareLenght> Keyword;

to method with parameter of type:

std::set<std::string>*

the difference in those types is the lack of CompareLenght in the Get_Word parameter type. You could make this mathod a template method to accept any type of KeyWord, or add overloaded method. Or simply change Get_Word to:

inline void Get_Word(std::set<std::string,CompareLenght> *Set_Of_Word); 
                                          ^^^^^^^^^^^^^

but you would have to then propagate this change to your Parole_Univoche or copy Set_Of_Word manually like that:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top