Program using inheritance

Hey guys, having some trouble with my program. I'm getting and error "No operator matches these operands". Not sure how to fix it. Below is my code. Thanks for any help.

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
  #include <iostream>
#include <string>
#include <vector>

#include "Header.h"
using namespace std;

int main ()
{
	vector <Employee*> EmployeeInfo;
	
	string name;
	double salary;
	string department;

	cout << "Please enter Manager name: ";
	getline(cin, name);
	cout << "Please enter Manager salary: ";
	cin >> salary;
	cout << "Please enter Manager department: ";
	cin >> department;
	cout << endl;

	EmployeeInfo[0] = new Manager(name, salary, department);

	cout << "Please enter executive name: ";
	getline(cin, name);
	cout << "Please enter Executive salary: ";
	cin >> salary;
	cout << "Please enter Executive department: ";
	cin >> department;


	EmployeeInfo[1] = new Manager(name, salary, department);

	cout << "Manager Name: " << EmployeeInfo[0]->print();
	cout << "Manager Name: " << EmployeeInfo[1]->print();

}


Header:
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
#ifndef HEADER_H
#define HEADER_H

#include <string>

using namespace std;

class Employee
{
public:
   /**
      Constructs an employee with empty name and no salary.
   */
   Employee();
   /**
      Constructs an employee with a given name and salary.
      @param employee_name the employee name
      @param initial_salary the initial salary
   */
   Employee(string employee_name, double initial_salary);
   /**
      Sets the salary of this employee.
      @param new_salary the new salary value
   */
   void set_salary(double new_salary);
   /**
      Gets the salary of this employee.
      @return the current salary
   */
   double get_salary() const;
   /**
      Gets the name of this employee.
      @return the employee name
   */
   string get_name() const;


   virtual void print() const;
private:
   string name;
   double salary;
};

class Manager : public Employee
{
public:
	Manager();
	Manager(string name, double salary, string dept);
	string get_department() const;
	virtual void print() const;

private:
	string department;
};

class Executive : public Manager
{
public:
	virtual void print() const;
private:
	string department;
};

#endif 


Implementation:
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
#include "Header.h"
#include <iostream>
#include <string>
using namespace std;

Employee::Employee()
{  
   salary = 0;
}

Employee::Employee(string employee_name, double initial_salary)
{  
   name = employee_name;
   salary = initial_salary;
}

void Employee::set_salary(double new_salary)
{  
   salary = new_salary;
}

double Employee::get_salary() const
{  
   return salary;
}

string Employee::get_name() const
{  
   return name;
}

Manager::Manager(string name, double salary, string dept)
	: Employee(name, salary)
{
	department = dept;
}

string Manager::get_department() const
{
	return department;
}

void Manager::print() const
{
	cout << "Name : " << get_name();
	cout << "Salary : " << get_salary();
	cout << "Department : " << get_department();
}

void Executive::print() const
{
	cout << "Executive ";
	cout << "Name : " << get_name();
	cout << "Salary : " << get_salary();
	cout << "Department : " << get_department();
}


Thanks again for any help.
can you give us a line number? Your compiler should tell you it (or at least near the actual problematic line).

never mind i had a look.
1
2
	cout << "Manager Name: " << EmployeeInfo[0]->print();
	cout << "Manager Name: " << EmployeeInfo[1]->print();


Your print method returns a void, and your operator<< has no idea how to handle this.

edit2: then you need to sort out your inheritence properly. (your employee does not have a print)
Last edited on
Any idea on how to fix this? Should I change the void to something else?
Also, it should probably be
EmployeeInfo.push_back(new Manager(name, salary, department));
instead of
EmployeeInfo[0] = new Manager(name, salary, department);
since EmployeeInfo is initialized to be empty (and operator[] doesn't expand it).

Same with your other one (on line 34).

To fix your other error, just do
36
37
38
39
cout << "Manager Name: ";
EmployeeInfo[0]->print();
cout << "Manager Name: ";
EmployeeInfo[1]->print();

Also, don't forget to delete your pointers afterwards, or you'll get a memory leak.

Oh yeah, and your classes should probably all have virtual destructors.
Last edited on
implement some kind of print method for employee first. then just call this:

EmployeeInfo[0]->print();
EmployeeInfo[1]->print();


I just deleted the cout statements cause I realized I didn't need them. But now i'm getting another error. unresolved externam symbol "public : virtual void Employee::print(void) const.
i've already answered what you need to do to fix that.
Are you compiling/linking your implementation file?
nothing to do with that.
Yeah I'm linking fine. I thought the virtual made it so I don't have to have a print function? My professor is pretty horrible at teaching us anything so I'm sorry if I don't sound so learned in it. She didn't say anything on the assignment about implementing a print method in the Employee class.
Yea i can see what you're trying to do, but at the end of the day you have a Employee class that does not inherit from anything, you've declared prototype for print() in your header, so your linker is trying to find the implementation of it in your Employee class in the .cpp.


You might like this:
https://www.cs.bu.edu/teaching/cpp/inheritance/intro/

Give a good (and very relevant) explanation I think. Although i've only skimmed over it :)
Last edited on
Thanks much I'll take a look at that. I appreciate the help, guys. I have to talk to her about this class cause she was great last semester and she sucks now. I get nothing out of her classes, and then she dumps these assignments on us and we don't know how to do it.
Topic archived. No new replies allowed.