Guys, I understand that I am doing something wrong, but cannot seem to find it.May you please help.
This is the error I am getting.Please,
1 2 3 4 5 6 7 8 9
EnglishTranslatorMachine.o: In function `EnglishTranslatorMachine::recieve(std::string, TranslatorMachine*)':
EnglishTranslatorMachine.cpp:(.text+0x203): undefined reference to `typeinfo for FrenchTranslatorMachine'
FrenchTranslatorMachine.o: In function `FrenchTranslatorMachine::FrenchTranslatorMachine()':
FrenchTranslatorMachine.cpp:(.text+0x1f): undefined reference to `vtable for FrenchTranslatorMachine'
FrenchTranslatorMachine.o: In function `FrenchTranslatorMachine::FrenchTranslatorMachine(ConferenceMediator*)':
FrenchTranslatorMachine.cpp:(.text+0x49): undefined reference to `vtable for FrenchTranslatorMachine'
GermanTranslatorMachine.o: In function `GermanTranslatorMachine::recieve(std::string, TranslatorMachine*)':
GermanTranslatorMachine.cpp:(.text+0x2fe): undefined reference to `typeinfo for FrenchTranslatorMachine'
collect2: error: ld returned 1 exit status
The "undefined reference to vtable" errors are caused by the compiler not having a .cpp file for the virtual lookup table for your objects. Basically, have a .cpp file associated with those classes, even if all that it contains is an empty destructor or something. That should solve those problems.
The undefined refernce to "typeinfo" errors are caused by your virtual functions not having implementations. To avoid this, either give them an empty implementation or declare them as pure virtual functions (i.e. func() = 0;)
If pos is greater than the string length, or if subpos is greater than str's length, an out_of_range exception is thrown.
Basically, that means that in one of your msg.replace, it has not found a letter that you want it too. This means that it returns 1 past the end of your string (basic_string::end()) and the replace function takes that as an error.
I would recommend instead saving the iterator to a variable first, testing the variable to see if it is beyond the end of range, and IF THAT IS SO then replace with the appropriate term.
msg.find(d) .find() returns the position in msg of the first match of d. If there is no match, it will return std::basic_string::npos: http://www.cplusplus.com/reference/string/basic_string/npos/
So, let's reconsider this line: msg.replace(f,m.length() - 1, "thankyou"); If f equals std::basic_string::npos, replace() will not be able to search, because it would start replacing at a value which is not a valid position within the string. So, the following will check if find() returns a valid position within the string or not:
1 2 3
size_t f = msg.find(m);
if (f != std::basic_string::npos)
msg.replace(f,m.length() - 1, "thankyou");