Singly Linked List - help with error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Singly Linked List - help with error

 
0
  #1
Feb 4th, 2009
Hey all. I have worked on this in a previous post, but because of the huge difference in work needed, i have reposted it here.

First off, i created a project that would input data from keyboard of a student, this student had name,ssn,dob,school id information that was needed. After inputting this data, you could add classes for this singly student... into an array of objects that included (course name, course semester, course year, and course grade). having done this with a basic array, i had set the array to be size 10 (very limiting if student has, say 11 classes!). Sooo..

For our second project we have to do a linked list of the course objects. Therefore, we can add classes to a single student for as long as we have the memory to store it all!

Here is an error i am getting in my class definition for the student (declaring a linked list):

1>driver.cpp
1>c:\users\tony\documents\visual studio 2008\projects\project2\student.h(27) : error C2143: syntax error : missing ';' before '<'
1>c:\users\tony\documents\visual studio 2008\projects\project2\student.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\visual studio 2008\projects\project2\student.h(27) : error C2238: unexpected token(s) preceding ';'
1>Student.cpp
1>c:\users\tony\documents\visual studio 2008\projects\project2\student.h(27) : error C2143: syntax error : missing ';' before '<'
1>c:\users\tony\documents\visual studio 2008\projects\project2\student.h(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\visual studio 2008\projects\project2\student.h(27) : error C2238: unexpected token(s) preceding ';'

This is using Visual Studio Pro 2008.

I will post all needed .h and .cpp files that are in the project. I had split them up for ease of read purposes, and per class requirements.

Please could someone explain what this error means? Am i declaring it incorrectly? Have i made a mistake elsewhere?

I have more questions to follow - but first off it would be good to get my linked list defined correctly first!

Thanks all

PS - the SinglyLinkedList.h file is the newest 'addition' to my project - i had this all working flawlessly previously (besides im sure my quirky programming!!!!)
Attached Files
File Type: h 340.h (438 Bytes, 14 views)
File Type: cpp courseInfo.cpp (4.3 KB, 8 views)
File Type: h courseInfo.h (992 Bytes, 8 views)
File Type: cpp driver.cpp (4.4 KB, 8 views)
File Type: cpp Person.cpp (4.0 KB, 12 views)
File Type: h Person.h (948 Bytes, 9 views)
File Type: h SinglyLinkedList.h (431 Bytes, 15 views)
File Type: cpp Student.cpp (3.0 KB, 8 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 69
Reputation: h3xc0de is an unknown quantity at this point 
Solved Threads: 12
h3xc0de h3xc0de is offline Offline
Junior Poster in Training

Re: Singly Linked List - help with error

 
0
  #2
Feb 4th, 2009
could you post the code to the "student.h" file
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Singly Linked List - help with error

 
0
  #3
Feb 4th, 2009
That would help huh? Sorry! My apologies!

Should be 'working' now in terms of needed files to attempt a compile!
Attached Files
File Type: h Student.h (815 Bytes, 12 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 69
Reputation: h3xc0de is an unknown quantity at this point 
Solved Threads: 12
h3xc0de h3xc0de is offline Offline
Junior Poster in Training

Re: Singly Linked List - help with error

 
0
  #4
Feb 4th, 2009
The error is on line 27 in students.h

SinglyLinkedList<CourseInfo> courses;

It doesn't know what a SinglyLinkedList is because you haven't defined it yet in your SinglyLinkedList.h

If you add

template<typename T>
class SinglyLinkedList
{


};

to your SinglyLinkedList.h it should compile fine!
Last edited by h3xc0de; Feb 4th, 2009 at 5:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Singly Linked List - help with error

 
0
  #5
Feb 4th, 2009
I thought i did have it: (SinglyLinkedList.h)

  1. template <class ItemType>
  2. class SinglyLinkedList
  3. {
  4. public:
  5. SinglyLinkedList() { head = tail = 0;}
  6. ~SinglyLinkedList();
  7. int isEmpty() {return head == 0} // checks to see if list is empty
  8. void addToHead(ItemType);
  9. void delteNode(ItemType);
  10. bool isInList(ItemType) const;
  11.  
  12. private:
  13. int count; // variable stores # of elements in list
  14. nodeSLL<ItemType> *head; //pointer to first node in list
  15. nodeSLL<ItemType> *tail; //pointer to last node in list
  16. };

*** There is more to the .h - than what is listed here, but this is the class definition that you thought i was missing correct???***
This is correct right?

Am i missing something else --- i thought i had that and it was set up correctly!?!

Thanks for the help so far! Please keep helping if possible!
Last edited by tones1986; Feb 4th, 2009 at 6:05 pm. Reason: change info for .h file
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Singly Linked List - help with error

 
0
  #6
Feb 4th, 2009
The above post is a big mistake. I was referencing the incorrect singlylinkedlist.h file when trying to compile. the file i was using at the time had no class definition...obvious mistake there! i will upload the new / updated singlylinkedlist.h file here.

new errors, only a couple of them i am getting now:

error C2955: 'nodeSLL' : use of class template requires template argument list
see declaration of 'nodeSLL'
hile compiling class template member function 'SinglyLinkedList<ItemType>::~SinglyLinkedList(void)'
1> with
1> [
1> ItemType=CourseInfo
1> ]
hile compiling class template member function 'SinglyLinkedList<ItemType>::~SinglyLinkedList(void)'

Attached is full error log:

thanks in advance
Attached Files
File Type: doc errors 11.doc (25.5 KB, 1 views)
File Type: h SinglyLinkedList.h (2.3 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 69
Reputation: h3xc0de is an unknown quantity at this point 
Solved Threads: 12
h3xc0de h3xc0de is offline Offline
Junior Poster in Training

Re: Singly Linked List - help with error

 
0
  #7
Feb 4th, 2009
The errors are coming from the for loop in this
  1. template <class ItemType>
  2. SinglyLinkedList<ItemType>::~SinglyLinkedList()
  3. {
  4.  
  5. for (nodeSLL *p; !isEmpty(); )
  6. {
  7. p = head->next;
  8. delete head;
  9. head = p;
  10. }
  11. }

so you might want to check out some information on for loops.
Also, nodeSLL* p should be nodeSLL<ItemType>* p
Last edited by h3xc0de; Feb 4th, 2009 at 6:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 69
Reputation: h3xc0de is an unknown quantity at this point 
Solved Threads: 12
h3xc0de h3xc0de is offline Offline
Junior Poster in Training

Re: Singly Linked List - help with error

 
0
  #8
Feb 4th, 2009
An easier way of doing the destructor is to use a while loop instead of a for loop

  1. template <class ItemType>
  2. SinglyLinkedList<ItemType>::~SinglyLinkedList()
  3. {
  4. while(head != 0)
  5. {
  6. nodeSLL<ItemType>* p = head->next;
  7. delete head;
  8. head = p;
  9. }
  10. }
Last edited by h3xc0de; Feb 4th, 2009 at 6:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Singly Linked List - help with error

 
0
  #9
Feb 5th, 2009
Hey guys - thanks a ton for all the help so far. Im getting there, at least so i think.

My next question concerns my input. I have to read in from a file and create the student object, and set the students class list all from a given text file. I have done this (at least the namessn,dob,gender, etc) in the cxase 0 of my driver.cpp file.

input file:
  1. John Smith 333222333 01/01/80 M z11119
  2. CS240 Fall 2009 A
  3. CS333 Spring 2009 B
  4. HIST450 Fall 2009 C

My problem is that i unsure how to read my class list - i think a while loop like this would work:

  1. while (inFile)
  2. {
  3. //read in class info (courseName, courseYear, courseSemester, courseGrade)
  4. // be able to read in as many as needed - and add them to the linked list set up in Student.h
  5. }

But how would i this work? I would have to create a new node at the start of the loop, and then i would send a function each value... but would i still be using my setSemester etc methods? Because i will now be using my linked list...

So for example, i should have:

Start:
LinkedList ->
Node1--
Name = cs240
Semester = Spring
Year = 2009
Grade = A
Next -> Node2
Name = cs340
Semester = Fall
Year = 2008
Grade = C
Next -> Node 3....

etc ... is this my correct logic in thinking this would be possible the way i have set up my linked list?

If so, is it as simple as using my set methods?

Thanks for help and the ideas to you all
Last edited by tones1986; Feb 5th, 2009 at 1:15 am. Reason: uploaded new singlylinkedlist.h file - due to incorrect destructor
Attached Files
File Type: cpp driver.cpp (4.0 KB, 2 views)
File Type: h SinglyLinkedList.h (2.3 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Singly Linked List - help with error

 
0
  #10
Feb 5th, 2009
Alright guys - i have figured out my last question, and as always have come across a myriad of other things i am unsure of. First off, i am getting a couple of errors i am unsure to their meaning:

(this is a warning, not an error, but need to get it rid of anyway):
Student.h:14, from driver.cpp In file included from Student.h:14, from driver.cpp
\SinglyLinkedList.h [Warning] no newline at end of file

And here are my other errors ... i hope someone can understand what i am TRYING to do, and what my variable and stuff are:

47: SinglyLinkedList.h there are no arguments to `addToHead' that depend on a template parameter, so a declaration of `addToHead' must be available
47 SinglyLinkedList.h (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
SinglyLinkedList.h In member function `nodeSLL<ItemType>* SinglyLinkedList<ItemType>::r_deleteNode(const ItemType&, nodeSLL<ItemType>*)':
83 SinglyLinkedList.h expected primary-expression before '*' token
83 SinglyLinkedList.h `next' undeclared (first use this function)

At the present time, i have worked on the following:

In driver.cpp:
I have worked on getting a case 0 so that i can read in from a text file (i will post this file with all data in it here). I have been able to read in and print the correct student name, ssn, etc. But i was hoping i could get input as to the way i did the classID, semester, grade, year, etc.
Case 0:
- i hope that you can see what i am doing by calling the addCourse() function, which i want to send a CourseInfo object to, this function will then call, addToHead() passing it the object, which will add the CourseInfo object to the head of my linked list. Is this possible do you think in what i have tried?

I appreciate all the help you can give me, and i hope you understand what i am trying to do!

- Please know you will have to change the location of my .txt file in the driver.cpp to your needs - as it curently lists my directory information.
Attached Files
File Type: h 340.h (438 Bytes, 5 views)
File Type: cpp courseInfo.cpp (4.3 KB, 11 views)
File Type: h courseInfo.h (992 Bytes, 6 views)
File Type: cpp driver.cpp (4.5 KB, 10 views)
File Type: txt file.txt (40 Bytes, 8 views)
File Type: cpp Person.cpp (4.0 KB, 5 views)
File Type: h Person.h (948 Bytes, 7 views)
File Type: h SinglyLinkedList.h (2.5 KB, 11 views)
File Type: cpp Student.cpp (2.9 KB, 13 views)
File Type: h Student.h (815 Bytes, 7 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC