| | |
Abstract Class member function problem
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi to everybody! Here I paste some simple exercise code that I wrote. The errors are pasted from the compiler log at the end of the source code. The add() function looks ok to me and I can't understand what the compiler wants from me :o Thanks!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <list> using namespace std; class Worker { protected: string fname, lname; string address; unsigned number; public: inline virtual void add(DepBoss* new_boss, list<Worker*>& M) = 0; inline virtual void print(void) { cout << " Name: " << fname << " " << lname; cout << " Address: " << address; cout << " Number: " << number; } Worker(string fn, string ln, string addr, unsigned n) :fname((string)fn), lname((string)ln), address((string)addr) { if(n < 0) throw "Scheisse in Worker Constructor!"; else number = n; }; virtual ~Worker(){}; }; class DepBoss : public Worker { private: short level; int podchineni; public: inline virtual void add(DepBoss* new_boss, list<Worker*>& M); inline virtual void print(void); DepBoss(string fn, string ln, string addr, unsigned n, short lvl, int pod) :Worker(fn, ln, addr, n) { if(lvl < 0 && lvl > 5) throw "Scheisse in DepBoss Constructor(lvl)"; else level = lvl; if(pod < 0 && pod > 100) throw "Scheisse in DepBoss Constructor(pod)"; else podchineni = pod; }; virtual ~DepBoss(){}; }; inline void DepBoss::add(DepBoss* new_boss, list<Worker*>& M) { M.push_front(new_boss); } void DepBoss::print(void) { cout << " DepBosses:" << endl; Worker::print(); cout << "\n Level of clearance: " << level; cout << "\n Number of podchineni: " << podchineni; } void print_list(list<Worker*>& w) { for(list<Worker*>::const_iterator I=w.begin(); I!=w.end(); ++I) (*I)->print(); } int main(void) { static string fn, ln, addr; static unsigned num; static short lvl; static int pod; try { cout << "\n Please, input first name: "; cin >> fn; cout << "\n Please, input last name : "; cin >> ln; cout << "\n Please, input DepBoss' address: "; cin >> addr; cout << "\n Please, input DepBoss' number: "; cin >> num; cout << "\n Please, input DepBoss' clearance level: "; cin >> lvl; cout << "\n Please, input DepBoss' number of podchineni: "; cin >> pod; DepBoss Boss("Toni", "Montana", "Cubastr. 13", 1, 0, 50); list<Worker*> L; L.push_front(&Boss); DepBoss* Boss2 = new DepBoss(fn, ln, addr, num, lvl, pod); Boss2->add(Boss2, L); print_list(L); delete Boss2; } catch(char *str) { cout << "\n Erroroneus inputs given: " << str << ".... Exiting." << endl; return -1; } return 0; } /* tbook.cpp:13: error: variable or field `add' declared void tbook.cpp:13: error: `add' declared as a `virtual' field tbook.cpp:13: error: `add' declared as an `inline' field tbook.cpp:13: error: expected `;' before '(' token make.exe: *** [tbook.o] Error 1 Execution terminated */
Hello Freemind:
I took a look at your code and actually attempted to run it in an empty default Visual Studio 6 project. It didn't compile but with different errors. The errors I recieved were around the 'add' function as well however.
Anyhow, with only a minor change I was able to compile and run your program. All I did was change the following three lines to use the base 'Worker' class instead of the derivied 'DepBoss' class: (each bold line replaces the line above it)
The reason I think the compiler was complaining is because you cannot use a derived class in a base class as you did. You can however use an instance of, or in this example a pointer to, a base class in derived classes.
Hope this helps, and if you would like a copy of the 'mini project' I created, let me know. Good luck!
Incidently, here was the ouput of the program:
I took a look at your code and actually attempted to run it in an empty default Visual Studio 6 project. It didn't compile but with different errors. The errors I recieved were around the 'add' function as well however.
Anyhow, with only a minor change I was able to compile and run your program. All I did was change the following three lines to use the base 'Worker' class instead of the derivied 'DepBoss' class: (each bold line replaces the line above it)
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Worker {
protected:
string fname, lname;
string address;
unsigned number;
public:
//inline virtual void add(DepBoss* new_boss, list<Worker*>& M) = 0;
inline virtual void add(Worker* new_boss, list<Worker*>& M) = 0;
inline virtual void print(void) {
cout << " Name: " << fname << " " << lname;
cout << " Address: " << address;
cout << " Number: " << number;
}
Worker(string fn, string ln, string addr, unsigned n)
:fname((string)fn), lname((string)ln), address((string)addr) {
if(n < 0)
throw "Scheisse in Worker Constructor!";
else
number = n;
};
virtual ~Worker(){};
};
class DepBoss : public Worker {
private:
short level;
int podchineni;
public:
//inline virtual void add(DepBoss* new_boss, list<Worker*>& M);
inline virtual void add(Worker* new_boss, list<Worker*>& M);
inline virtual void print(void);
DepBoss(string fn, string ln, string addr, unsigned n, short lvl, int pod)
:Worker(fn, ln, addr, n) {
if(lvl < 0 && lvl > 5)
throw "Scheisse in DepBoss Constructor(lvl)";
else
level = lvl;
if(pod < 0 && pod > 100)
throw "Scheisse in DepBoss Constructor(pod)";
else
podchineni = pod;
};
virtual ~DepBoss(){};
};
//inline void DepBoss::add(DepBoss* new_boss, list<Worker*>& M)
inline void DepBoss::add(Worker* new_boss, list<Worker*>& M) {
M.push_front(new_boss);
}
void DepBoss::print(void) {
cout << " DepBosses:" << endl;
Worker::print();
cout << "\n Level of clearance: " << level;
cout << "\n Number of podchineni: " << podchineni;
}
void print_list(list<Worker*>& w) {
for(list<Worker*>::const_iterator I=w.begin(); I!=w.end(); ++I)
(*I)->print();
}
int main(void) {
static string fn, ln, addr;
static unsigned num;
static short lvl;
static int pod;
try {
cout << "\n Please, input first name: "; cin >> fn;
cout << "\n Please, input last name : "; cin >> ln;
cout << "\n Please, input DepBoss' address: "; cin >> addr;
cout << "\n Please, input DepBoss' number: "; cin >> num;
cout << "\n Please, input DepBoss' clearance level: "; cin >> lvl;
cout << "\n Please, input DepBoss' number of podchineni: "; cin >> pod;
DepBoss Boss("Toni", "Montana", "Cubastr. 13", 1, 0, 50);
list<Worker*> L;
L.push_front(&Boss);
DepBoss* Boss2 = new DepBoss(fn, ln, addr, num, lvl, pod);
Boss2->add(Boss2, L);
print_list(L);
delete Boss2;
}
catch(char *str) {
cout << "\n Erroroneus inputs given: " << str << ".... Exiting." << endl;
return -1;
}
return 0;
}
/*
tbook.cpp:13: error: variable or field `add' declared void
tbook.cpp:13: error: `add' declared as a `virtual' field
tbook.cpp:13: error: `add' declared as an `inline' field
tbook.cpp:13: error: expected `;' before '(' token
make.exe: *** [tbook.o] Error 1
Execution terminated
*/The reason I think the compiler was complaining is because you cannot use a derived class in a base class as you did. You can however use an instance of, or in this example a pointer to, a base class in derived classes.
Hope this helps, and if you would like a copy of the 'mini project' I created, let me know. Good luck!
Incidently, here was the ouput of the program:
•
•
•
•
Please, input first name: Michael
Please, input last name : R
Please, input DepBoss' address: Bayview
Please, input DepBoss' number: 11111
Please, input DepBoss' clearance level: 2
Please, input DepBoss' number of podchineni: 5
DepBosses:
Name: Michael R Address: Bayview Number: 11111
Level of clearance: 2
Number of podchineni: 5 DepBosses:
Name: Toni Montana Address: Cubastr. 13 Number: 1
Level of clearance: 0
Number of podchineni: 50
•
•
•
•
Originally Posted by TomcatMCAD
Hello Freemind:
I took a look at your code and actually attempted to run it in an empty default Visual Studio 6 project. It didn't compile but with different errors. The errors I recieved were around the 'add' function as well however.
Anyhow, with only a minor change I was able to compile and run your program. All I did was change the following three lines to use the base 'Worker' class instead of the derivied 'DepBoss' class: (each bold line replaces the line above it)
#include <iostream> #include <string> #include <list> using namespace std; class Worker { protected: string fname, lname; string address; unsigned number; public: //inline virtual void add(DepBoss* new_boss, list<Worker*>& M) = 0; inline virtual void add(Worker* new_boss, list<Worker*>& M) = 0; inline virtual void print(void) { cout << " Name: " << fname << " " << lname; cout << " Address: " << address; cout << " Number: " << number; } Worker(string fn, string ln, string addr, unsigned n) :fname((string)fn), lname((string)ln), address((string)addr) { if(n < 0) throw "Scheisse in Worker Constructor!"; else number = n; }; virtual ~Worker(){}; }; class DepBoss : public Worker { private: short level; int podchineni; public: //inline virtual void add(DepBoss* new_boss, list<Worker*>& M); inline virtual void add(Worker* new_boss, list<Worker*>& M); inline virtual void print(void); DepBoss(string fn, string ln, string addr, unsigned n, short lvl, int pod) :Worker(fn, ln, addr, n) { if(lvl < 0 && lvl > 5) throw "Scheisse in DepBoss Constructor(lvl)"; else level = lvl; if(pod < 0 && pod > 100) throw "Scheisse in DepBoss Constructor(pod)"; else podchineni = pod; }; virtual ~DepBoss(){}; }; //inline void DepBoss::add(DepBoss* new_boss, list<Worker*>& M) inline void DepBoss::add(Worker* new_boss, list<Worker*>& M) { M.push_front(new_boss); } void DepBoss::print(void) { cout << " DepBosses:" << endl; Worker::print(); cout << "\n Level of clearance: " << level; cout << "\n Number of podchineni: " << podchineni; } void print_list(list<Worker*>& w) { for(list<Worker*>::const_iterator I=w.begin(); I!=w.end(); ++I) (*I)->print(); } int main(void) { static string fn, ln, addr; static unsigned num; static short lvl; static int pod; try { cout << "\n Please, input first name: "; cin >> fn; cout << "\n Please, input last name : "; cin >> ln; cout << "\n Please, input DepBoss' address: "; cin >> addr; cout << "\n Please, input DepBoss' number: "; cin >> num; cout << "\n Please, input DepBoss' clearance level: "; cin >> lvl; cout << "\n Please, input DepBoss' number of podchineni: "; cin >> pod; DepBoss Boss("Toni", "Montana", "Cubastr. 13", 1, 0, 50); list<Worker*> L; L.push_front(&Boss); DepBoss* Boss2 = new DepBoss(fn, ln, addr, num, lvl, pod); Boss2->add(Boss2, L); print_list(L); delete Boss2; } catch(char *str) { cout << "\n Erroroneus inputs given: " << str << ".... Exiting." << endl; return -1; } return 0; } /* tbook.cpp:13: error: variable or field `add' declared void tbook.cpp:13: error: `add' declared as a `virtual' field tbook.cpp:13: error: `add' declared as an `inline' field tbook.cpp:13: error: expected `;' before '(' token make.exe: *** [tbook.o] Error 1 Execution terminated */
The reason I think the compiler was complaining is because you cannot use a derived class in a base class as you did. You can however use an instance of, or in this example a pointer to, a base class in derived classes.
Hope this helps, and if you would like a copy of the 'mini project' I created, let me know. Good luck!
Incidently, here was the ouput of the program:
Thanks for the answer and the time that you took! However the program runs quite fine with the minor change of adding the line "class DepBoss;" before "class Worker {...}" (special thanks to zyrus).
•
•
•
•
Originally Posted by freemind
Thanks for the answer and the time that you took! However the program runs quite fine with the minor change of adding the line "class DepBoss;" before "class Worker {...}" (special thanks to zyrus).
also, Since the Worker class contains a function that uses DepBoss, which inturn is derived from Worker, how could one ever really create another usable derived class, say for instance 'class Employee : public Worker'. I understand that you can get the compiler to accept another derived class but from a structural point of view... you couldn't do much with it. and if thats the case... why not create one single class instead of using inhertance.
It seems like the solution of predefining the derived class so you can use it specifically in the base class, while syntactically correct, may still not necessarily be the correct answer from a design point of view and to that end, I'm curious to know what else if anything you intend to do with the program and how your Worker and DepBoss classes will fit into those plans.
-Michael
![]() |
Similar Threads
- problem with an abstract class (C#)
- problem instantiating a subclass of abstract class (C)
- Ask Function problem?thanks (C)
- Reading an input file as a class memeber function (C++)
Other Threads in the C++ Forum
- Previous Thread: MFC Compiling Error Question
- Next Thread: simple image
Views: 3458 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





