Cannot solve Error

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


Here are my classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class TranslatorMachine;

class ConferenceMediator
{
public:
	ConferenceMediator(void);
	ConferenceMediator( int _maximumLinkedMachines);
	~ConferenceMediator(void);

	void linkTranslator( TranslatorMachine* tm );
	void send( string msg, TranslatorMachine* tm );

private:

	TranslatorMachine** machines;
	int numLinkedMachines;
	int maximumLinkedMachines;
};

#endif














//The send and recieve are implemented in the cpp
class EnglishTranslatorMachine :
	public TranslatorMachine
{
public:
	EnglishTranslatorMachine(void);
	EnglishTranslatorMachine( ConferenceMediator* act);
	~EnglishTranslatorMachine(void);

	virtual void send( string msg );
	virtual void recieve( string msg, TranslatorMachine* tm );

};





//The cpp where I implement the virtyual functions

EnglishTranslatorMachine :: EnglishTranslatorMachine (){}
	
EnglishTranslatorMachine  :: ~EnglishTranslatorMachine (){}
	
void EnglishTranslatorMachine :: send(string msg)
	
	
void EnglishTranslatorMachine :: recieve(string msg, TranslatorMachine *tm)
	{
	}







class ConferenceMediator;

class TranslatorMachine
{
public:
	TranslatorMachine(void){}
	TranslatorMachine( ConferenceMediator* _mediator );
	virtual ~TranslatorMachine(void){}

	virtual void send( string msg ) =0;
	virtual void recieve( string msg, TranslatorMachine* from)=0;

protected:
	ConferenceMediator* mediator;

};

#endif


//my makefile
main: *.o
	g++ -static -Wall -Werror *.o -o main
	
*.o: *.cpp
	g++ -c *.cpp


These are my classes.Please help

Edited:
Last edited on
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;)

Hope this helps.
Thanks solved that.But now I am getting an error that says

1
2
3
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::replace
Aborted


I want to search and replace a word.Here is the code that gives me an error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

void EnglishTranslatorMachine :: recieve(string msg, TranslatorMachine *tm)
	{
			string m = "merci";
			string c = "courtois";
			string h = "hospitality";
			string d = "danke";
			string u = "unternehmen";
			string k = "konnen";
			
			for(int i = 0; i <msg.length();i++)
		{
		if(dynamic_cast<FrenchTranslatorMachine*>(tm))
		{
			
			msg.replace(msg.find(m), m.length(), "thankyou");
			msg.replace(msg.find(c), c.length(), "gracious");
			msg.replace(msg.find(h), h.length(), "hospitality");
		}
		
		if(dynamic_cast<GermanTranslatorMachine *>(tm))
		{
			msg.replace(msg.find(d), d.length(), "thankyou");
			msg.replace(msg.find(u), u.length(), "business");
			msg.replace(msg.find(k), k.length(), "lets");
		}
	}
		
		cout<<"English hear: "<<msg <<endl;
	}
http://www.cplusplus.com/reference/string/basic_string/replace/ says:
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.

Hope this helps.
It still gives me that.What am I really doing wrong
I updated my function but still getting the error.Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

	
		size_t f = msg.find(m);
		if(dynamic_cast<FrenchTranslatorMachine*>(tm))
		{
			size_t f = msg.find(m);
			msg.replace(f,m.length() - 1, "thankyou");
			size_t s = msg.find(c);
			msg.replace(s, c.length() - 1, "gracious");
			size_t z = msg.find(h);
			msg.replace(z, h.length() - 1, "hospitality");
		}
		
Storing the return value is not checking the return value.
So are you suggesting that I have a if statement for each checking if its 0 or 1.Please I am lost now.You are really loosing me
closed account (o3hC5Di1)
Hi there,

Let's clarify a little bit:

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");



Hope that makes sense?

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.