Student looking for more help. `ListNode' does not name a type

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.
 
M

mlimber

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>

but in any case said:
#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;

This is C-style. The simpler C++-style would be:

struct ListNode
{
ListEntry data;
ListNode *next;
};
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:

Extraneous (perhaps left over from some deletions you made for this
post?).
};
#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)

Your ListNode class is a member of the JobCollection class, and so
outside of that class, you must qualify it:

JobCollection::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 [snip]
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.

See above.

Cheers! --M
 
V

Victor Bazarov

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:
[...]

class JobCollection
{
public:
typedef Job ListEntry;

typedef struct list_node_def
{
ListEntry data;
struct list_node_def *next;
}ListNode;

Why this C construct? Why not do it the C++ way

struct ListNode
{
ListEntry data;
ListEntry *next;
};

???
[...irrelevant...]

ListNode *SetPosition(int pos, ListType *list);


private:


};
#endif

</code>

my .cpp file looks like this:

[...irrelevant...]
ListNode JobCollection::SetPosition(int pos, ListType *list)

You need

JobCollection::ListNode JobCollection::SetPosition ...

The reason is that in this scope where you are defining the function,
the 'ListNode' *by itself* is not declared. It only exists in its parent
class definition.
{
[...irrelevant...]
}

The error I am getting is:
81 Z:\Trent\COSC202H\Assignment1B\JobCollection.cpp `ListNode' does
not name a type

[..]

V
 
S

sandy

mlimber said:
In C++ prefer <cstdlib>, but in any case, it doesn't look like you use
this here.

I have tried to use it but I get an error:
stdlib: No such file or directory.
This is C-style. The simpler C++-style would be:

struct ListNode
{
ListEntry data;
ListNode *next;
};

I assume this is a function of cstdlib (which I cannot seem to use...)
Extraneous (perhaps left over from some deletions you made for this
post?).

Extraneous because I am first trying to get it all to work, then I will
worry about what should be private.
<cstdio> is preferred.

Thanks. I have made that change.
Your ListNode class is a member of the JobCollection class, and so
outside of that class, you must qualify it:

JobCollection::ListNode
JobCollection::SetPosition(int pos, ListType *list)

Okay, this is where my ignorance shows... Isn't my .cpp file that I am
creating PART of the class?
See above.

Cheers! --M

Thanks for your help. It compiles now.
 
J

jjds101

I have tried to use it but I get an error:
stdlib: No such file or directory.


I assume this is a function of cstdlib (which I cannot seem to use...)
No, it's not library dependent, just the preferred C++ syntax. Using
the typdef is necessary for a struct definition in C but not in C++.

Extraneous because I am first trying to get it all to work, then I will
worry about what should be private.


Thanks. I have made that change.


Okay, this is where my ignorance shows... Isn't my .cpp file that I am
creating PART of the class?
Doesn't matter if it's in your .cpp or .h file -- you are physically
outside of the block

class MyClass {

};

so for definitions outside of that block you need to use:

MyClass::MyStruct MyClass::MyFunction() {
//definition
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top