this is my Header file
#include <string>
using namespace std;
class Employee
{
private:
typedef struct Emp
{ int person;
int salary;
Emp* next;
}* top;
top head;
top setnode();
void removefirst(top&);
void findnode(top& ,top&, int);
void removemiddle(top& ,top& );
void removelast(top &,top &);
void print(Emp* );
void clearlist(top &);
public:
Employee();
~Employee();
void insert(top &);
void menu(top &);
void deletenode(top& );
};
#endif
and this is my cpp file
#include "Employee.h"
Employee::Employee() //constructor function
{
head={NULL};
}
Employee::~Employee() //destructor
{
}
void Employee::menu(top &head)
{
char input;
do
{ //system("cls");
cout << "\t\t\tMain Menu\n\n\n";
cout << "\t\t1- (I)nsert\n";
cout << "\t\t2- (D)elete item\n";
cout << "\t\t3- (P)rint all\n";
cout << "\t\t4- (C)lear\n";
cout << "\t\t5- (E)xit\n";
cout << "\t\t\tChoice ?";
cin>> input;
input = toupper(input);
switch (input)
{ case '1':
case 'I': insert(head);
break;
case '2':
case 'D': if( head != NULL)
deletenode(head);
else
cout <<"list is empty"<<endl;
break;
case '3':
case 'P': if (head != NULL)
print(head);
else
cout << "\nList is empty"<<endl;
break;
case '4':
case 'C': if (head != NULL)
clearlist(head);
break;
}
cin.get();
} while ((input != '5') && (input != 'E'));
}
void Employee::insert(top &hd)
{
if (hd == NULL)
hd = setnode();
else
{ top temp = hd;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = setnode();
}
}
top Employee::setnode()
{ top node = new Emp;
cout << "Id Number "; cin >> node->person;
cout << "Salary" ; cin >> node->salary;
node->next = NULL;
return node;
}
void Employee::deletenode(top &head)
{ int idtodelete;
top prev = head,curr=head;
cout << "Id to Delete";cin >> idtodelete;
if (head->person == idtodelete)
removefirst(head);
else
{
findnode(prev,curr, idtodelete);
if(curr->next != NULL)
removemiddle(prev,curr);
else
if (curr->person == idtodelete)
removelast(prev, curr);
else
cout << idtodelete << "Is not in the list";
}
}
void Employee::removefirst(top &head)
{ top first=head;
head=head->next;
delete(first);
}
void Employee::findnode(top &prev,top &curr, int idtodelete)
{
while (curr->next != NULL && curr->person != idtodelete)
{
prev = curr;
curr = curr->next;
}
}
void Employee::removemiddle(top &prev,top &curr)
{
prev->next = curr->next;
delete(curr); //delete any where in the middle
}
void Employee::removelast(top &prev,top &curr)
{ prev->next = NULL;
delete(curr); //delete last
}
void Employee::print(Emp *pt)
{ if (pt == NULL)
cout << "NULL"<<endl;
else
{ cout<< pt->person<<endl;
cout<< pt->salary<<endl<<endl;
print(pt->next); // recursive
}
}
void Employee::clearlist(top &pt)
{ top temp;
while (pt != NULL)
{ temp = pt;
pt = pt->next;
delete temp;
}
}
now i get one error that says top does not name a type in the set node function under the insert one.
C:\Users\Acosta\Desktop\C++ programs\CIS 260\Project 3\Employee.cpp At global scope:
70 4 C:\Users\Acosta\Desktop\C++ programs\CIS 260\Project 3\Employee.cpp [Error] 'top' does not name a type
31 C:\Users\Acosta\Desktop\C++ programs\CIS 260\Project 3\Makefile.win recipe for target 'Employee.o' failed
help i been trying to solve this for days now
Declare typedef struct Emp
{
int person;
int salary;
Emp* next;
}
*top;
top head;
before class declaration .
Since declaration is inside the class compiler will take it as Employee::Emp
and will not match the function return type