S
sandy
I am creating a class (or so I hope) which is to be a JobCollection,
linked list of 'Job'. Job is another class already created.
JobCollection is just the linked list manager.
I have to have this separated into a .h and .cpp file. The code I am
attempting to put into the class is code that I have used before, but
simply put into the same file/class with the rest of the application.
My header looks like this:
<code>
#ifndef JobCollection_h
#define JobCollection_h
#include "Job.h"
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class JobCollection
{
public:
typedef Job ListEntry;
typedef struct list_node_def
{
ListEntry data;
struct list_node_def *next;
}ListNode;
typedef struct listtype
{
int count;
ListNode *head;
}ListType;
/******************************************************
Constructor
Inputs:
Outputs:
Notes: Creates a new empty linked list with no nodes.
************************************************************/
JobCollection();
/******************************************************************************
ClearList
Inputs: Pointer to a list
Outputs: None
Notes: Empties the list of all nodes.
*******************************************************************************/
void ClearList(ListType *list);
/********************************************************************************
CheckListEmpty
Inputs: Pointer to a list
Outputs: boolean - True = Empty
********************************************************************************/
bool CheckListEmpty(ListType *list);
/*********************************************************************************
ListSize
Inputs: pointer to a List
Outputs: Number of elements in the list
*********************************************************************************/
int ListSize(ListType *list);
/******************************************************************************
CreateList
Inputs:
Outputs: None
Notes: Creates an empty linked list with no nodes
******************************************************************************/
void CreateList(ListType *list);
/********************************************************************************
SetPosition
Inputs: pos - position within the list to move to, *list
pointer
Outputs: ListNode
Notes: Move the pointer to a specific node in the list, by
position
*******************************************************************************/
ListNode *SetPosition(int pos, ListType *list);
private:
};
#endif
</code>
my .cpp file looks like this:
<code>
#include "JobCollection.h"
#include "Job.h"
#include <string>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
using namespace std;
/******************************************************
Constructor
Inputs:
Outputs:
Notes: Creates a new empty linked list with no nodes.
************************************************************/
JobCollection::JobCollection()
{
ListType *thisList;
CreateList(thisList);
}
/******************************************************************************
CreateList
Inputs:
Outputs: None
Notes: Creates an empty linked list with no nodes
******************************************************************************/
void JobCollection::CreateList(ListType *list)
{
/* initialize list to empty*/
list->head = NULL;
list->count = 0;
}
/******************************************************************************
ClearList
Inputs: Pointer to a list
Outputs: None
Notes: Empties the list of all nodes.
*******************************************************************************/
void JobCollection::ClearList(ListType *list)
{
//re-set list to empty /
ListNode *currptr, *tmpptr;
currptr = list->head;
while(currptr != NULL)
{
tmpptr = currptr;
currptr = currptr->next;
free(tmpptr);
}
list->count = 0;
list->head = NULL;
}
/*********************************************************************************
ListSize
Inputs: pointer to a List
Outputs: Number of elements in the list
*********************************************************************************/
int JobCollection::ListSize(ListType *list)
{
/* return the number of occupied elements in the list*/
return list->count;
}
/********************************************************************************
SetPosition
Inputs: pos - position within the list to move to, *list
pointer
Outputs: ListNode
Notes: Move the pointer to a specific node in the list, by
position
*******************************************************************************/
ListNode JobCollection::SetPosition(int pos, ListType *list)
{
ListNode *currptr;
int counter = 0;
/* make sure pos is within the valid range */
if( (pos < 0) || (pos >= list->count))
{
cout << "*** Error - pos out of range\n";
}
else
{
/*move the pointer forward pos positions*/
currptr = list->head;
while( (counter < pos) && (currptr != NULL))
{
currptr = currptr->next;
counter++;
}
}
if (currptr == NULL)
{
cout << "*** Error - Problem in setPosition\n";
}
else
{
return currptr;
}
}
The error I am getting is:
81 Z:\Trent\COSC202H\Assignment1B\JobCollection.cpp `ListNode' does not
name a type
Now in my header I have this snippet:
<code>
typedef struct list_node_def
{
ListEntry data;
struct list_node_def *next;
}ListNode;
</code>
the code CAUSING the error is in the cpp file:
<code>
/********************************************************************************
SetPosition
Inputs: pos - position within the list to move to, *list
pointer
Outputs: ListNode
Notes: Move the pointer to a specific node in the list, by
position
*******************************************************************************/
ListNode JobCollection::SetPosition(int pos, ListType *list)
{
ListNode *currptr;
int counter = 0;
/* make sure pos is within the valid range */
if( (pos < 0) || (pos >= list->count))
{
cout << "*** Error - pos out of range\n";
}
else
{
/*move the pointer forward pos positions*/
currptr = list->head;
while( (counter < pos) && (currptr != NULL))
{
currptr = currptr->next;
counter++;
}
}
if (currptr == NULL)
{
cout << "*** Error - Problem in setPosition\n";
}
else
{
return currptr;
}
}
</code>
So, since I have a data type called ListNode in the header file, I
don't understand why the compiler says I don't.
Any help to solve the problem would be appreciated. Help in
understanding WHY would be even MORE appreciated.
Thanks.
linked list of 'Job'. Job is another class already created.
JobCollection is just the linked list manager.
I have to have this separated into a .h and .cpp file. The code I am
attempting to put into the class is code that I have used before, but
simply put into the same file/class with the rest of the application.
My header looks like this:
<code>
#ifndef JobCollection_h
#define JobCollection_h
#include "Job.h"
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
class JobCollection
{
public:
typedef Job ListEntry;
typedef struct list_node_def
{
ListEntry data;
struct list_node_def *next;
}ListNode;
typedef struct listtype
{
int count;
ListNode *head;
}ListType;
/******************************************************
Constructor
Inputs:
Outputs:
Notes: Creates a new empty linked list with no nodes.
************************************************************/
JobCollection();
/******************************************************************************
ClearList
Inputs: Pointer to a list
Outputs: None
Notes: Empties the list of all nodes.
*******************************************************************************/
void ClearList(ListType *list);
/********************************************************************************
CheckListEmpty
Inputs: Pointer to a list
Outputs: boolean - True = Empty
********************************************************************************/
bool CheckListEmpty(ListType *list);
/*********************************************************************************
ListSize
Inputs: pointer to a List
Outputs: Number of elements in the list
*********************************************************************************/
int ListSize(ListType *list);
/******************************************************************************
CreateList
Inputs:
Outputs: None
Notes: Creates an empty linked list with no nodes
******************************************************************************/
void CreateList(ListType *list);
/********************************************************************************
SetPosition
Inputs: pos - position within the list to move to, *list
pointer
Outputs: ListNode
Notes: Move the pointer to a specific node in the list, by
position
*******************************************************************************/
ListNode *SetPosition(int pos, ListType *list);
private:
};
#endif
</code>
my .cpp file looks like this:
<code>
#include "JobCollection.h"
#include "Job.h"
#include <string>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
using namespace std;
/******************************************************
Constructor
Inputs:
Outputs:
Notes: Creates a new empty linked list with no nodes.
************************************************************/
JobCollection::JobCollection()
{
ListType *thisList;
CreateList(thisList);
}
/******************************************************************************
CreateList
Inputs:
Outputs: None
Notes: Creates an empty linked list with no nodes
******************************************************************************/
void JobCollection::CreateList(ListType *list)
{
/* initialize list to empty*/
list->head = NULL;
list->count = 0;
}
/******************************************************************************
ClearList
Inputs: Pointer to a list
Outputs: None
Notes: Empties the list of all nodes.
*******************************************************************************/
void JobCollection::ClearList(ListType *list)
{
//re-set list to empty /
ListNode *currptr, *tmpptr;
currptr = list->head;
while(currptr != NULL)
{
tmpptr = currptr;
currptr = currptr->next;
free(tmpptr);
}
list->count = 0;
list->head = NULL;
}
/*********************************************************************************
ListSize
Inputs: pointer to a List
Outputs: Number of elements in the list
*********************************************************************************/
int JobCollection::ListSize(ListType *list)
{
/* return the number of occupied elements in the list*/
return list->count;
}
/********************************************************************************
SetPosition
Inputs: pos - position within the list to move to, *list
pointer
Outputs: ListNode
Notes: Move the pointer to a specific node in the list, by
position
*******************************************************************************/
ListNode JobCollection::SetPosition(int pos, ListType *list)
{
ListNode *currptr;
int counter = 0;
/* make sure pos is within the valid range */
if( (pos < 0) || (pos >= list->count))
{
cout << "*** Error - pos out of range\n";
}
else
{
/*move the pointer forward pos positions*/
currptr = list->head;
while( (counter < pos) && (currptr != NULL))
{
currptr = currptr->next;
counter++;
}
}
if (currptr == NULL)
{
cout << "*** Error - Problem in setPosition\n";
}
else
{
return currptr;
}
}
The error I am getting is:
81 Z:\Trent\COSC202H\Assignment1B\JobCollection.cpp `ListNode' does not
name a type
Now in my header I have this snippet:
<code>
typedef struct list_node_def
{
ListEntry data;
struct list_node_def *next;
}ListNode;
</code>
the code CAUSING the error is in the cpp file:
<code>
/********************************************************************************
SetPosition
Inputs: pos - position within the list to move to, *list
pointer
Outputs: ListNode
Notes: Move the pointer to a specific node in the list, by
position
*******************************************************************************/
ListNode JobCollection::SetPosition(int pos, ListType *list)
{
ListNode *currptr;
int counter = 0;
/* make sure pos is within the valid range */
if( (pos < 0) || (pos >= list->count))
{
cout << "*** Error - pos out of range\n";
}
else
{
/*move the pointer forward pos positions*/
currptr = list->head;
while( (counter < pos) && (currptr != NULL))
{
currptr = currptr->next;
counter++;
}
}
if (currptr == NULL)
{
cout << "*** Error - Problem in setPosition\n";
}
else
{
return currptr;
}
}
</code>
So, since I have a data type called ListNode in the header file, I
don't understand why the compiler says I don't.
Any help to solve the problem would be appreciated. Help in
understanding WHY would be even MORE appreciated.
Thanks.