PDA

View Full Version : C++ help!!!


DrDeThuong
10-10-2002, 11:08 PM
Hi, I need some help ...

There is a base class employee, and 2 others class (manager, laborer) which are derived from class employee.

=================================
=================================

// file employee.h
// class employee defination

class employee {
// body here
};

--------------------------
// file employee.cpp
// implementation for class employee.

#include "employee.h"
// blab blab blab...

// end of employee.cpp

=================================
=================================

// file manager.h
// class manager defination

#include "employee.h"
class manager : public employee{
// body here
};

------------------------------------
// file manager.cpp
// implementation for class manager.

#include "manager.h"
// blab blab blab...

// end of manager.cpp

=================================
=================================

// file laborer.h
// class laborer defination

#include "employee.h"
class laborer: public employee{
// body here
};

------------------------------------
// file laborer.cpp
// implementation for class laborer.

#include "laborer.h"
// blab blab blab...

// end of laborer.cpp


=================================
=================================

// file driver.cpp
// testing and usage for classess

#include "laborer.h"
#include "manager.h"

int main() {
manage m1,m2;
laborer b1,m2;
// more go here for testing ...
return 0;
}

***************************************
***************************************
NOTE: I can NOT put everything in the one big file. My professor do NOT allow that. Class defination and implementation must be seperate. So there are total 7 files (2X3 class = 6, plus 1 for driver use for testing)


PROBLEM:
when I try to compile this problem, I get the following error
".. redefination of class employee .. "

As I can see, the driver.cpp includes "manager.h" and "laborer.h", and since both file "manager.h" and "laborer.h" include ( #include "employee.h" ) therefore it generate the error. Hence redefination of class employee.


So, how can I fix this problem? I'm using Dev-C++
Does compiler has anything to do with it? Since my professsor gonna use his own compiler to compile my program, so hopefully compiler will not be the problem.

Please help.. this is urgent..
Thank alot

If u can, please leave your AIM or Yahoo M.. so I can contact u .. just in case

Trung Quốc
10-11-2002, 04:51 PM
there shouldn't be a problem with that. Even if you use #include in every single file you have the compiler will know that you already defined that class (assumed you defined that class correctly. :))

If you use this to define each of your classes (*.h) then it should run correctly.


#ifndef HEAPSORT_H
#define HEAPSORT_H

******************

#endif


If you still need help, zip the whole thing and let me take a look at it. :0

Sorry I can't help earlier, however as I posted the rule a while back that I am to busy with school and everything in my life so that if members need help, he/she should post about 3-4 days in advance so that I and others can give hints so that he/she can work on it. :)

QT

DrDeThuong
10-11-2002, 06:10 PM
Hi,

I attach the source files in zip.. please help me take a look at it
Thanks alot

//CommunityMember is base class
// Employee and Alumnus are derived class from CommunityMember

Trung Quốc
10-11-2002, 06:41 PM
please read the last post really careful. All you have to do is but the if stetement so that your compiler would not define that class again

For intance if you have a file commember.h

then you should start that file by

#ifndef COMMEM_H
#define COMMEM_H

here is everything for your class

#endif

that code above tell the compiler COMMEM_H is not define then define it, else just skip it.

By doing that, you program should run fine. Then read the post in the other thread to correct the problem in which you program didn't ask for the department.


QT

DrDeThuong
10-11-2002, 07:49 PM
thanks alot.. it work now
my professor didnt tell me about the #ifndef and #define directive yet :-(

but the the department still cause error :-(