Linker Error in derived class initialization

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Linker Error in derived class initialization

 
0
  #1
Jul 31st, 2005
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..

  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <list>
  6. using namespace std;
  7. class TelB;
  8. class AB { // Address Book
  9. protected:
  10. string name;
  11. string address;
  12. string land;
  13. public:
  14. virtual void add(list<TelB*>&);
  15. virtual void del(list<TelB*>&);
  16. virtual void show() const {
  17. cout << "\n Name: " << name << "\n Address: " << address;
  18. cout << "\n Land: " << land;
  19. };
  20. AB(string n, string a, string l){
  21. name = (string) n;
  22. address = (string) a;
  23. land = (string) l;
  24. }
  25. virtual ~AB(){};
  26. };
  27. class TelB : public AB { // Telephone Book
  28. private:
  29. string number;
  30. enum Type { Mobile = 1, Static = 2 };
  31. Type Tel_type;
  32. public:
  33. virtual void add(list<TelB*>&);
  34. virtual void del(list<TelB*>&);
  35. virtual void show() const;
  36. TelB(string n, string a, string l, string num, int type)
  37. :AB(n, a, l) {
  38.  
  39. string tmp;
  40. string MobileBG = "888", MobileDE = "177";
  41. string StaticBG = "00359", StaticDE = "0049";
  42.  
  43. // Adding pre-dial stuff for the numbers(for convenience)
  44. if(l == "Bulgaria")
  45. if(type == 1)
  46. tmp = StaticBG + MobileBG;
  47. else
  48. tmp = StaticBG;
  49. if(l == "Germany")
  50. if(type == 1)
  51. tmp = StaticDE + MobileDE;
  52. else
  53. tmp = StaticDE;
  54.  
  55. number = tmp + " / " + num;
  56. }
  57. virtual ~TelB(){};
  58. };
  59. void TelB::add(list<TelB*>& PB) {
  60. string n, a, l, num;
  61. short t;
  62.  
  63. cout << "\n Please input name: "; cin >> n;
  64. cout << "\n Please input address: "; cin >> a;
  65. cout << "\n Please input land(Bulgaria or Germany)?: "; cin >> l;
  66. cout << "\n Please input number: "; cin >> num;
  67. cout << "\n What type of phone is this(1 - Mobile, 2 - Static): "; cin >> t;
  68.  
  69. TelB* new_entry = new TelB(n, a, l, num, (Type) t);
  70. PB.push_front(new_entry);
  71. delete new_entry;
  72. }
  73. void TelB::show() const {
  74. AB::show();
  75. cout << "\n Number: " << number;
  76. cout << "\n Phone type: " << Tel_type;
  77. }
  78. void TelB::del(list<TelB*>& PB){
  79. cout << "\n Please input an entry number to be removed: ";
  80. long n, j=0;
  81. cin >> n;
  82. for(list<TelB*>::iterator I=PB.begin(); ;++I) {
  83. j++;
  84. if(n == j) {
  85. PB.erase(I);
  86. cout << "\n Entry deleted.\n";
  87. break;
  88. }
  89. if(I==PB.end()) {
  90. cout << "\n No such entry found...\n";
  91. break;
  92. }
  93. }
  94. }
  95. short printmenu(void) {
  96. cout << "\n\n\n";
  97. cout << "\n 1. Add to phone book";
  98. cout << "\n 2. Delete from phone book";
  99. cout << "\n 3. Show entries";
  100. cout << "\n 4. Quit";
  101. cout << "\n Your Choice?: ";
  102.  
  103. short choice;
  104. cin >> choice;
  105.  
  106. return choice;
  107. }
  108. void print_list(list<TelB*>& w) {
  109. long j=0;
  110. for(list<TelB*>::const_iterator I=w.begin(); I!=w.end(); ++I) {
  111. j++; (*I)->show();
  112. cout << "\n Entry number: " << j << endl;
  113. }
  114. }
  115. int main(void) {
  116.  
  117. TelB Default("Me", "Some Address", "Bulgaria", "777877", 2);
  118. list<TelB*> Phone_book;
  119.  
  120. for(;;) {
  121. switch(printmenu()) {
  122. case 1: Default.add(Phone_book);
  123. break;
  124. case 2: Default.del(Phone_book);
  125. break;
  126. case 3: print_list(Phone_book);
  127. break;
  128. case 4: return 1;
  129. break;
  130. default: cout << "\n Please choose a number from 1 to 4 for your "
  131. << "corresponding choice." << endl;
  132. break;
  133. }
  134. }
  135.  
  136. return 0;
  137. }

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Linker Error in derived class initialization

 
0
  #2
Jul 31st, 2005
These two member functions are not defined:
  1. virtual void add(list<TelB*>&);
  2. virtual void del(list<TelB*>&);
You need to either provide them with a body, or make them pure.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Linker Error in derived class initialization

 
0
  #3
Jul 31st, 2005
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
  1.  
  2.  
  3. TelB* new_entry = new TelB(n, a, l, num, t);
  4. PB.push_front(new_entry);
  5. 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Linker Error in derived class initialization

 
0
  #4
Jul 31st, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Linker Error in derived class initialization

 
0
  #5
Jul 31st, 2005
Here I paste the edited and fully working code in case somebody is interested in it.

  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <list>
  6.  
  7. using namespace std;
  8.  
  9. static const char* lala[] = {"Mobile", "Static"};
  10.  
  11. class TelB;
  12. class AB { // Address Book
  13. protected:
  14. string name;
  15. string address;
  16. string land;
  17. public:
  18. virtual void add(list<TelB>&) = 0;
  19. virtual void del(list<TelB>&) = 0;
  20. virtual void show() const {
  21. cout << "\n Name: " << name << "\n Address: " << address;
  22. cout << "\n Land: " << land;
  23. };
  24. AB(string n, string a, string l){
  25. name = (string) n;
  26. address = (string) a;
  27. land = (string) l;
  28. }
  29. virtual ~AB(){};
  30. };
  31.  
  32. class TelB : public AB { // Telephone Book
  33. private:
  34. string number;
  35. enum Type { Mobile = 1, Static = 2 };
  36. Type Tel_type;
  37. public:
  38. virtual void add(list<TelB>&);
  39. virtual void del(list<TelB>&);
  40. virtual void show() const;
  41. TelB(string n, string a, string l, string num, int type)
  42. :AB(n, a, l) {
  43.  
  44. string tmp;
  45. string MobileBG = "888", MobileDE = "177";
  46. string StaticBG = "00359", StaticDE = "0049";
  47.  
  48. // Adding pre-dial stuff for the numbers(for convenience)
  49. if(l == "Bulgaria")
  50. if(type == 1)
  51. tmp = StaticBG + MobileBG;
  52. else
  53. tmp = StaticBG;
  54. if(l == "Germany")
  55. if(type == 1)
  56. tmp = StaticDE + MobileDE;
  57. else
  58. tmp = StaticDE;
  59.  
  60. number = tmp + " / " + num;
  61. Tel_type = (Type) type;
  62. }
  63. virtual ~TelB(){};
  64. };
  65.  
  66. void TelB::add(list<TelB>& PB) {
  67. string n, a, l, num;
  68. short t;
  69.  
  70. cout << "\n Please input name: "; cin >> n;
  71. cout << "\n Please input address: "; cin >> a;
  72. cout << "\n Please input land(Bulgaria or Germany)?: "; cin >> l;
  73. cout << "\n Please input number: "; cin >> num;
  74. cout << "\n What type of phone is this(1 - Mobile, 2 - Static): "; cin >> t;
  75.  
  76. TelB* new_entry = new TelB(n, a, l, num, t);
  77. PB.push_front(*new_entry);
  78. delete new_entry;
  79. }
  80.  
  81. void TelB::show() const {
  82. AB::show();
  83. cout << "\n Number: " << number;
  84. cout << "\n Phone type: " << lala[(int)Tel_type-1];
  85. }
  86.  
  87. void TelB::del(list<TelB>& PB){
  88. cout << "\n Please input an entry number to be removed: ";
  89. long n, j=0;
  90. cin >> n;
  91. for(list<TelB>::iterator I=PB.begin(); ;++I) {
  92. j++;
  93. if(n == j) {
  94. PB.erase(I);
  95. cout << "\n Entry deleted.\n";
  96. break;
  97. }
  98. if(I==PB.end()) {
  99. cout << "\n No such entry found...\n";
  100. break;
  101. }
  102. }
  103. }
  104.  
  105. short printmenu(void) {
  106. cout << "\n\n\n";
  107. cout << "\n 1. Add to phone book";
  108. cout << "\n 2. Delete from phone book";
  109. cout << "\n 3. Show entries";
  110. cout << "\n 4. Quit";
  111. cout << "\n Your Choice?: ";
  112.  
  113. short choice;
  114. cin >> choice;
  115.  
  116. return choice;
  117. }
  118.  
  119. void print_list(list<TelB>& w) {
  120. long j=0;
  121. for(list<TelB>::const_iterator I=w.begin(); I!=w.end(); ++I) {
  122. j++; (I)->show();
  123. cout << "\n Entry number: " << j << endl;
  124. }
  125. }
  126.  
  127. int main(void) {
  128.  
  129. TelB Default("Me", "Some Address", "Bulgaria", "777877", 2);
  130. list<TelB> Phone_book;
  131.  
  132. for(;;) {
  133. switch(printmenu()) {
  134. case 1: Default.add(Phone_book);
  135. break;
  136. case 2: Default.del(Phone_book);
  137. break;
  138. case 3: print_list(Phone_book);
  139. break;
  140. case 4: return 1;
  141. break;
  142. default: cout << "\n Please choose a number from 1 to 4 for your "
  143. << "corresponding choice." << endl;
  144. break;
  145. }
  146. }
  147.  
  148. return 0;
  149. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2688 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC