| | |
Linker Error in derived class initialization
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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..
Thanks!
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..
C++ Syntax (Toggle Plain Text)
#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!
These two member functions are not defined:
You need to either provide them with a body, or make them pure.
C++ Syntax (Toggle Plain Text)
virtual void add(list<TelB*>&); virtual void del(list<TelB*>&);
New members chased away this month: 5
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
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
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
C++ Syntax (Toggle Plain Text)
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
Here I paste the edited and fully working code in case somebody is interested in it.
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- Help with derived class and arithmetic in C++ (C++)
- A MFC linker error (ASP.NET)
- derived class (C)
- Linker Error>Undefined reference error to xyz (C++)
Other Threads in the C++ Forum
- Previous Thread: Function help
- Next Thread: a problme need you kindly help
Views: 2688 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






