Here I paste a simple address book/telephone book. The linker spits the following error:
g++.exe derived2.o -o "Derived2.exe" -L"C:/Dev-Cpp/lib"
derived2.o(.text$_ZN2ABD2Ev[AB::~AB()]+0x3a):derived2.cpp: undefined reference to `vtable for AB'
derived2.o(.text$_ZN2ABC2ESsSsSs[AB::AB(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0x3d):derived2.cpp: undefined reference to `vtable for AB'
collect2: ld returned 1 exit status
make.exe: *** [Derived2.exe] Error 1
Execution terminated

I would be thankful for ideas on what is wrong..

#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std; 
class TelB;
class AB {									  // Address Book
	  protected:
				string name;
				string address;
				string land;
	  public:
				virtual void add(list<TelB*>&);
				virtual void del(list<TelB*>&);
				virtual void show() const {
						cout << "\n Name: " << name << "\n Address: " << address;
						cout << "\n Land: " << land;
				};
				AB(string n, string a, string l){
							 name = (string) n;
							 address = (string) a;
							 land = (string) l;
				}
				virtual ~AB(){};
};
class TelB : public AB {					   // Telephone Book
	  private:
			  string number;
			  enum Type { Mobile = 1, Static = 2 };
			  Type Tel_type;
	  public:
			 virtual void add(list<TelB*>&);
			 virtual void del(list<TelB*>&);
			 virtual void show() const;
			 TelB(string n, string a, string l, string num, int type)
						 :AB(n, a, l) {
						 
						 string tmp;
						 string MobileBG = "888", MobileDE = "177";
						 string StaticBG = "00359", StaticDE = "0049";
						 
						 // Adding pre-dial stuff for the numbers(for convenience)
						 if(l == "Bulgaria")
								 if(type == 1)
											   tmp = StaticBG + MobileBG;
								 else
											   tmp = StaticBG;
						 if(l == "Germany")
								 if(type == 1)
											   tmp = StaticDE + MobileDE;
								 else
											   tmp = StaticDE;
						 
						 number = tmp + " / " + num;
			 }
			 virtual ~TelB(){};
};
void TelB::add(list<TelB*>& PB) {
	 string n, a, l, num;
	 short t;
	 
	 cout << "\n Please input name: "; cin >> n;
	 cout << "\n Please input address: "; cin >> a;
	 cout << "\n Please input land(Bulgaria or Germany)?: "; cin >> l;
	 cout << "\n Please input number: "; cin >> num;
	 cout << "\n What type of phone is this(1 - Mobile, 2 - Static): "; cin >> t;
	 
	 TelB* new_entry = new TelB(n, a, l, num, (Type) t);
	 PB.push_front(new_entry);
	 delete new_entry;
}
void TelB::show() const {
	 AB::show();
	 cout << "\n Number: " << number;
	 cout << "\n Phone type: " << Tel_type;
}
void TelB::del(list<TelB*>& PB){
	 cout << "\n Please input an entry number to be removed: ";
	 long n, j=0;
	 cin >> n;
	 for(list<TelB*>::iterator I=PB.begin(); ;++I) {
			 j++;
			 if(n == j) {
				  PB.erase(I);
				  cout << "\n Entry deleted.\n";
				  break;
			 }
			 if(I==PB.end()) {
				  cout << "\n No such entry found...\n";
				  break;
			 }
	 }
}
short printmenu(void) {
	  cout << "\n\n\n";
	  cout << "\n 1. Add to phone book";
	  cout << "\n 2. Delete from phone book";
	  cout << "\n 3. Show entries";
	  cout << "\n 4. Quit";
	  cout << "\n Your Choice?: ";
	  
	  short choice;
	  cin >> choice;
	  
	  return choice;
}
void print_list(list<TelB*>& w) {
	 long j=0;
	 for(list<TelB*>::const_iterator I=w.begin(); I!=w.end(); ++I) {
			  j++; (*I)->show();
			  cout << "\n Entry number: " << j << endl;
	 }
}
int main(void) {
	
	TelB Default("Me", "Some Address", "Bulgaria", "777877", 2);
	list<TelB*> Phone_book;
	
	for(;;) {
				switch(printmenu()) {
						case 1: Default.add(Phone_book);
							 break;
						case 2: Default.del(Phone_book);
							 break;
						case 3: print_list(Phone_book);
							 break;
						case 4: return 1;
							 break;
						default: cout << "\n Please choose a number from 1 to 4 for your "
									  << "corresponding choice." << endl;
							  break;
				}
	}
	
	return 0;
}

Thanks!

Recommended Answers

All 4 Replies

These two member functions are not defined:

virtual void add(list<TelB*>&);
virtual void del(list<TelB*>&);

You need to either provide them with a body, or make them pure.

Thanks.
Get's the job done but crashes when the list has to be shown. Since there is nothing specific about showing it I consider a possible error when adding an entry. This is only an asumption, though, but I am not quite sure if this

TelB* new_entry = new TelB(n, a, l, num, t);
	 PB.push_front(new_entry);
	 delete new_entry;

is correct. Isn't it pushing_front a wrong thing ? Since devcpp for unknown reason does't want to add debugging information to the project I can't try to debug it. So.. I ask ;)

I withdraw the last question. Sorry for the delay. Just changed list<TelB*> to list<TelB> and added some changes on 1-2 places. Now it works correctly. Sorry if I have taken from somebody's time!

Here I paste the edited and fully working code in case somebody is interested in it.

#include <iostream>
#include <string>
#include <list>

using namespace std; 

static const char* lala[] = {"Mobile", "Static"};

class TelB;
class AB {									  // Address Book
	  protected:
				string name;
				string address;
				string land;
	  public:
				virtual void add(list<TelB>&) = 0;
				virtual void del(list<TelB>&) = 0;
				virtual void show() const {
						cout << "\n Name: " << name << "\n Address: " << address;
						cout << "\n Land: " << land;
				};
				AB(string n, string a, string l){
							 name = (string) n;
							 address = (string) a;
							 land = (string) l;
				}
				virtual ~AB(){};
};

class TelB : public AB {					   // Telephone Book
	  private:
			  string number;
			  enum Type { Mobile = 1, Static = 2 };
			  Type Tel_type;
	  public:
			 virtual void add(list<TelB>&);
			 virtual void del(list<TelB>&);
			 virtual void show() const;
			 TelB(string n, string a, string l, string num, int type)
						 :AB(n, a, l) {
						 
						 string tmp;
						 string MobileBG = "888", MobileDE = "177";
						 string StaticBG = "00359", StaticDE = "0049";
						 
						 // Adding pre-dial stuff for the numbers(for convenience)
						 if(l == "Bulgaria")
								 if(type == 1)
											   tmp = StaticBG + MobileBG;
								 else
											   tmp = StaticBG;
						 if(l == "Germany")
								 if(type == 1)
											   tmp = StaticDE + MobileDE;
								 else
											   tmp = StaticDE;
						 
						 number = tmp + " / " + num;
						 Tel_type = (Type) type;
			 }
			 virtual ~TelB(){};
};

void TelB::add(list<TelB>& PB) {
	 string n, a, l, num;
	 short t;
	 
	 cout << "\n Please input name: "; cin >> n;
	 cout << "\n Please input address: "; cin >> a;
	 cout << "\n Please input land(Bulgaria or Germany)?: "; cin >> l;
	 cout << "\n Please input number: "; cin >> num;
	 cout << "\n What type of phone is this(1 - Mobile, 2 - Static): "; cin >> t;
	 
	 TelB* new_entry = new TelB(n, a, l, num, t);
	 PB.push_front(*new_entry);
	 delete new_entry;
}

void TelB::show() const {
	 AB::show();
	 cout << "\n Number: " << number;
	 cout << "\n Phone type: " << lala[(int)Tel_type-1];
}

void TelB::del(list<TelB>& PB){
	 cout << "\n Please input an entry number to be removed: ";
	 long n, j=0;
	 cin >> n;
	 for(list<TelB>::iterator I=PB.begin(); ;++I) {
			 j++;
			 if(n == j) {
				  PB.erase(I);
				  cout << "\n Entry deleted.\n";
				  break;
			 }
			 if(I==PB.end()) {
				  cout << "\n No such entry found...\n";
				  break;
			 }
	 }
}

short printmenu(void) {
	  cout << "\n\n\n";
	  cout << "\n 1. Add to phone book";
	  cout << "\n 2. Delete from phone book";
	  cout << "\n 3. Show entries";
	  cout << "\n 4. Quit";
	  cout << "\n Your Choice?: ";
	  
	  short choice;
	  cin >> choice;
	  
	  return choice;
}

void print_list(list<TelB>& w) {
	 long j=0;
	 for(list<TelB>::const_iterator I=w.begin(); I!=w.end(); ++I) {
			  j++; (I)->show();
			  cout << "\n Entry number: " << j << endl;
	 }
}

int main(void) {
	
	TelB Default("Me", "Some Address", "Bulgaria", "777877", 2);
	list<TelB> Phone_book;
	
	for(;;) {
				switch(printmenu()) {
						case 1: Default.add(Phone_book);
							 break;
						case 2: Default.del(Phone_book);
							 break;
						case 3: print_list(Phone_book);
							 break;
						case 4: return 1;
							 break;
						default: cout << "\n Please choose a number from 1 to 4 for your "
									  << "corresponding choice." << endl;
							  break;
				}
	}
	
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.