How to Inherite in C++

Hi I'm a C++ newbe,
I've created a simply project in Eclipse called Employee, it contains a main
and three classes one called Employee which has two subclasses VolEmp and TempEmp
I'm trying to enable access to both classes in my main, but I'm only able to include one of them without getting complaints, had assumed I should just write

#include "Employee.h"
#include "VolEmp.h"
#include "TempEmp.h" to enable access to all three classes but if i enable more than one of these I keep getting the following errors:
error: redefinition of `class Employee'
error: previous definition of `class Employee'
I include my full main and hope someone could please tell me how to enable access to all tree classes...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * main.cpp
 *
 *  Created on: 08-Nov-2008
 *      Author: Patrick Nevin: 06754155
 */

#include <iostream>
#include <fstream>
#include <vector>
//#include "Employee.h"
//#include "VolEmp.h"
#include "TempEmp.h"
using namespace std;

int main() {

	//read in the employee data file
	fstream empStream("emp.dat");
	//create a vector of Employee objects
	vector<Employee> workers;

	//used to hold the current word in the stream....
	string current;
	//as we iterate over each word in it
	while (empStream >> current) {
		//the current employee's name
		string name;
		//the  current employee's surname
		string s_name;
		//the current employee's wage
		double wage;
 //if the current word is TEMP read in info and create a new TempEmp object
		if (current == "TEMP") {
			empStream >> name;
			empStream >> s_name;
			empStream >> wage;

			Employee *e = new TempEmp(name, s_name, current);
			e->set_wage(wage);
			//And add it to the vector
			workers.push_back(*e);
			delete e;

		}
//if the current word is TEMP read in info and create a new TempEmp object
		if (current == "FULL") {
			empStream >> name;
			empStream >> s_name;
			empStream >> wage;

			Employee *em = new Employee(name, s_name, current);
			em->set_wage(wage);
			//And add it to the vector
			workers.push_back(*em);
			delete em;

		}
      //if the current word is VOL read in info and create a new VolEmp object
		if (current == "VOL") {
			empStream >> name;
			empStream >> s_name;
                        // Should be new VolEmp here but can olny include 
                        //either VolEmp of TempEmp but not both??
			Employee *emp = new Employee(name, s_name, current);
			emp->set_wage(wage);
			//And add it to the vector
			workers.push_back(*emp);
			delete emp;

		}
	}//end while()
	empStream.close();

	vector<Employee>::iterator it;
	for (it = workers.begin(); it != workers.end(); it++) {
		Employee current = *it;
		current.print();
	}

	return 0;
}
Last edited on
It's probably the fact that your headers are including other headers...try using header protection:

1
2
3
4
#ifndef HEADER_FILE
#define HEADER_FILE
//code
#endif 
Ta works now :-)
or use
 
#pragma once 

@wicketh: That would probably work on most compilers although it is technically non-standard, and any given compiler does not have to support it.
Topic archived. No new replies allowed.