I have a linked list with each node holding an employee object. The employee class has name and salary data. I am currently trying to create an overloaded ostream operator << that outputs my list of employees.
For example (should print out: Name1, Salary1, \n Name2, Salary2, \n etc.):
1 2
ListOfEmployee list1;
cout << list1;
My current code is giving me some errors even though the overloaded function is a friend so I am not sure why this is happening
1. identifier head is undefined
2. next is inaccessible
ostream& operator <<(ostream output, ListOfEmployee emp)
{
ListPtr current = head; // ERROR: identifier head is undefined
while (current->next != NULL) // ERROR: next is inaccessible
{
//output name and salary for employee nodes
}
return output;
}
In a function, the following variables exist:
1) Global variables that exist everywhere.
2) Variables passed in as parameters
3) Variables created in that function
4) In class member functions, class member variables
Which of these is "head" in the function above? None of them
Thanks for the reply mate. How is the head variable in this function different to the head var in the above function 'ListOfEmployee::getSalary(string name)'? This function can access head without any problems. That is what i can not understand
//.cpp functions
void ListOfEmployee::printList(ostream& output) const {
current = head;
while (current->next != NULL)
{
cout << "Employee name: " << current->emp.name << " ID: "<<current->emp.salary;
current = current->next;
}
}
ostream& operator<< (ostream& lhs, const ListOfEmployee& rhs)
{
rhs.printList(lhs);
return lhs;
}
//ListOfEmployee.h
#ifndef LISTOFEMPLOYEE_H
#define LISTOFEMPLOYEE_H
#include "EmployeeListNode.h"
#include <iostream>
usingnamespace std;
class ListOfEmployee
{
public:
ListOfEmployee();
~ListOfEmployee();
std::ostream& operator<< (std::ostream& lhs, const ListOfEmployee& rhs);
// ERROR: too many parameters for this operator function,
// function definition for 'operator<<' not found
void printList(std::ostream& output) const;
void insertAtFront(string, double);
void deleteMostRecent();
double getSalary(string name);
private:
EmployeeListNode* head;
EmployeeListNode* current;
EmployeeListNode* temp;
};
#endif
regarding who deallocates the dynamically allocates list nodes. im not quite sure what you're asking. my EmployeeListNode.cpp is as follows if that's what you mean:
Thanks for the reply mate. How is the head variable in this function different to the head var in the above function 'ListOfEmployee::getSalary(string name)'?
That's a class function. It's part of the ListOfEmployee class. A class function has access to all the member variables of that class.
Thanks repeater. Is there any way I could get 'friend ostream& operator <<..' function to have access to all the member variablse of the ListOfEmployee and EmployeeListNode classes? i.e. the head and next variables
I have tried lots of different things and I still can not get this code to work! I have also tried keskivertos way and got some errors. Some coding help would be appreciated :D
i have got it working, although I had to make the emp and next variables in the EmployeeListNode class public instead of private.. which is not ideal. Is there anyway to do it and keep the variables private?
It works perfect now thanks a lot guys. I made the operator a friend of both the List and Node classes. Also forward declared class ListOfEmployee for Node class.
Now i just need to do some research so i know what forward declaring a class means and why i done it!