|
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. | |
Share:
| sa***@murdocks.on.ca wrote:
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>
In C++ prefer <cstdlib>, but in any case, it doesn't look like you use
this here.
#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>
<cstdiois preferred.
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 | | | sa***@murdocks.on.ca wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | |
mlimber wrote:
#include "Job.h"
#include <stdlib.h>
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...)
private:
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.
>
<cstdiois preferred.
Thanks. I have made that change.
/************************************************** ******************************
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)
Okay, this is where my ignorance shows... Isn't my .cpp file that I am
creating PART of the class?
Any help to solve the problem would be appreciated. Help in
understanding WHY would be even MORE appreciated.
See above.
Cheers! --M
Thanks for your help. It compiles now. | | | sa***@murdocks.on.ca wrote:
mlimber wrote:
>>#include "Job.h" #include <stdlib.h>
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.
Did you miss the 'c' in front of 'stdlib' by any chance?
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | sa***@murdocks.on.ca wrote:
mlimber wrote:
#include "Job.h"
#include <stdlib.h>
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...)
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++.
private:
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.
<cstdiois preferred.
Thanks. I have made that change.
/************************************************** ******************************
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)
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
}
Any help to solve the problem would be appreciated. Help in
understanding WHY would be even MORE appreciated.
See above.
Cheers! --M
Thanks for your help. It compiles now.
| | |
Thank you.
I did miss the 'c'.
When I frustrated I get flustered.
Thanks.
Victor Bazarov wrote:
sa***@murdocks.on.ca wrote:
mlimber wrote:
>#include "Job.h" #include <stdlib.h>
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.
Did you miss the 'c' in front of 'stdlib' by any chance?
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
| | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by Sue |
last post: by
|
1 post
views
Thread by jhon02148 |
last post: by
|
2 posts
views
Thread by sarah |
last post: by
|
13 posts
views
Thread by na1paj |
last post: by
|
22 posts
views
Thread by Dave Cooke |
last post: by
|
3 posts
views
Thread by sandy@murdocks.on.ca |
last post: by
| |
4 posts
views
Thread by jjh5030@gmail.com |
last post: by
| | | | | | | | | | | |