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;
<strong>inline virtual void add(Worker* new_boss, list<Worker*>& M) = 0;</strong>
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);
<strong>inline virtual void add(Worker* new_boss, list<Worker*>& M);</strong>
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)
<strong>inline void DepBoss::add(Worker* new_boss, list<Worker*>& M)</strong> {
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