error!! please help!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Trie::addDictionary (string filename){
        fstream file("sae-sorted.dic", ios::in);
        string word;
        if(!file.is_open())
        
        {
                cout << "File not found!\n";
                return false;
        }        
        while (file >> word) 
        {
               *this += word;
        }
        return true;
}



error: no match for "operator+=" in "*(Trie*)this += word"

Someone please help!!
Last edited on
You haven't defined operator+= (with a string rhs) for class Trie.
I'm not understanding your answer. So, how to fix this?
What do you expect to happen at line 12?

What is the declaration of the class Trie?
I'm not understanding your answer. So, how to fix this?


1
2
3
4
5
Trie& Trie::operator += (std::string& word)
{
    myWord += word;
    return *this;
};



where myWord is a std::string object located within your class.
Well, I'm sorry but that didn't work. I'm trying to read words from a .txt letter by letter, using overload operator.
I'm not sure why you would want to overload the operator here anyway. Just add it to your string member. So, instead of this:
1
2
3
4
while (file >> word) 
        {
               *this += word;
        }


it would be something like this:
1
2
3
4
while (file >> word) 
        {
               myString += word;  //myString is a std::string object
        }


The std::string library has an operator that will take this syntax.
Last edited on
Still doesn't work.

This is what I add to class:
1
2
3
4
5
6
7
 string myWord;
     
     Trie& Trie::operator += (std::string& word)
    {
    myWord += word;
    return *this;
    } 


And this is my function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 bool Trie::addDictionary (string filename){
        fstream file("sae-sorted.dic", ios::in);
        string word;
        string myString;
        if(!file.is_open())
        
        {
                cout << "File not found!\n";
                return false;
        }        
        while (file >> word) 
        {
               myString += word;
        }
        return true;
} 


I get the error: error: extra qualification "Trie::" on member "operator+="
Please help!! =)
You are defining the member function inside the class, you shouldn't tell the class which it belongs to. (remove the `Trie::' part)

By the way, ¿do you know what a trie is?
Your attempt is quite disappointing.

To insert a word in a trie
1
2
3
4
5
6
7
8
9
10
11
12
void Trie::add_word( std::string word ){
   if( word.empty() ) //we are done inserting
      this->terminator = true; //we mark `end of word'

   subtrie[ word[0] ].add_word( word.substr(1) );
}

//structure
class Trie{
   bool terminator;
   map<char, Trie> subtrie;
};

`subtrie[ word[0] ]' would follow the branch that corresponds to the first letter (create it if it does not exists
This is the way I was instructed to do it, using this function and overload operator. I'm not trying to insert a word; I'm trying to read the words.


main.cpp:(.text._ZN4TrieC2Ev[_ZN4TrieC5Ev]+0x13): undefined reference to `vtable for Trie'
/tmp/ccQ979Q4.o: In function `Trie::~Trie()':
main.cpp:(.text._ZN4TrieD2Ev[_ZN4TrieD5Ev]+0x13): undefined reference to `vtable for Trie'
collect2: ld returned 1 exit status

What does this error even mean?

Last edited on
> I'm not trying to insert a word; I'm trying to read the words.
1
2
3
std::string word;
while( cin>>word )
   ; //do nothing 
Topic archived. No new replies allowed.