| | |
Parse Error Before 'else'
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
FIXED - I'm getting a parse error somewhere in this section of code before the else and I can't see it. Please help someone.
NEED HELP HERE -
Okay, I'm apparently getting a parse error somewhere in this code. Any help?
All the errors are listed as "Parse error before '.'" and are happening in the main function at the very end of the code. The first is inside the while statement.
NEED HELP HERE -
Okay, I'm apparently getting a parse error somewhere in this code. Any help?
All the errors are listed as "Parse error before '.'" and are happening in the main function at the very end of the code. The first is inside the while statement.
C++ Syntax (Toggle Plain Text)
// This is the implementation file for class List #include <iostream> #include <fstream> #include <stddef.h> // to access NULL #include "List.h" using namespace std; typedef NodeType* NodePtr; struct NodeType { ItemType item; NodePtr next; }; List::List() // Post: listPtr is set to NULL. { listPtr = NULL; } //*********************************************************** List::List(const List& otherList) // Copy-constructor for List. { NodeType* ptr1; NodeType* ptr2; if (otherList.listPtr == NULL) { listPtr = NULL; } else { listPtr = new NodeType; listPtr->item = otherList.listPtr->item; ptr1 = otherList.listPtr->next; ptr2 = listPtr; while (ptr1 != NULL) { ptr2->next = new NodeType; ptr2 = ptr2->next; ptr2->item = ptr1->item; ptr1 = ptr1->next; } ptr2->next = NULL; } } //*********************************************************** bool List::IsThere(ItemType item) const // Post: If item is in the list IsThere is // True; False, otherwise. { NodeType* location = listPtr; while (location != NULL) { if (item == listPtr->item ) { return true; } } return false; } //*********************************************************** void List::Insert(ItemType item) // Pre: item is not already in the list. // Post: item is the first item in the list. { NodeType* location; if (IsThere(item) == false) { location = new NodeType; location->item = item; location->next = listPtr; listPtr = location; } } //*********************************************************** void List::Delete(ItemType item) // Pre: item is in the list. // Post: item is no longer in the list. { NodeType* location = listPtr; NodeType* tempLocation; if (IsThere(item) == true) { tempLocation = location->next; listPtr = listPtr->next; } else { while (IsThere(item) == true) { location = location->next; } tempLocation = location->next; location->next = (location->next)->next; } delete tempLocation; } //*********************************************************** void List::Print() const // Post: Items on the list are printed on the screen. { NodeType* location = listPtr; while (location != NULL) { cout << location->item << endl; location = location->next; } } //*********************************************************** int List::Length() const // Post: Number of items have been counted; result returned. { NodeType* location = listPtr; int length = 0; while (location != NULL) { location = location->next; length++; } return length; } //*********************************************************** List::~List() // Post: All the components are deleted. { NodeType* tempPtr; while (listPtr != NULL) { tempPtr = listPtr; listPtr = listPtr->next; delete tempPtr; } } int main () { ifstream InData; InData.open ("int.dat"); while (InData) { List.Insert(InData); } int length = List.Length(); cout << "There are " << length << " many items in this list." << endl; List.Print(); system ("pause"); return 0; }
Last edited by DemonGal711; Oct 8th, 2008 at 1:51 pm.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
Your else is outside your while loop .. it might help to format your code more cleanly
C++ Syntax (Toggle Plain Text)
while (location != NULL){ if (item == listPtr->item ){ return true; } } // while ends // now we have an else without an if // statement else{ return false; }
•
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
Yeah, I realized that. Now I have another issue of more parse errors later in my program. I thought that was causing the rest, like a missing semi-colon or something. But apparently not.
All the errors are happening in the main function at the very end. The first is inside the while statement.
All the errors are happening in the main function at the very end. The first is inside the while statement.
C++ Syntax (Toggle Plain Text)
// This is the implementation file for class List #include <iostream> #include <fstream> #include <stddef.h> // to access NULL #include "List.h" using namespace std; typedef NodeType* NodePtr; struct NodeType { ItemType item; NodePtr next; }; List::List() // Post: listPtr is set to NULL. { listPtr = NULL; } //*********************************************************** List::List(const List& otherList) // Copy-constructor for List. { NodeType* ptr1; NodeType* ptr2; if (otherList.listPtr == NULL) { listPtr = NULL; } else { listPtr = new NodeType; listPtr->item = otherList.listPtr->item; ptr1 = otherList.listPtr->next; ptr2 = listPtr; while (ptr1 != NULL) { ptr2->next = new NodeType; ptr2 = ptr2->next; ptr2->item = ptr1->item; ptr1 = ptr1->next; } ptr2->next = NULL; } } //*********************************************************** bool List::IsThere(ItemType item) const // Post: If item is in the list IsThere is // True; False, otherwise. { NodeType* location = listPtr; while (location != NULL) { if (item == listPtr->item ) { return true; } } return false; } //*********************************************************** void List::Insert(ItemType item) // Pre: item is not already in the list. // Post: item is the first item in the list. { NodeType* location; if (IsThere(item) == false) { location = new NodeType; location->item = item; location->next = listPtr; listPtr = location; } } //*********************************************************** void List::Delete(ItemType item) // Pre: item is in the list. // Post: item is no longer in the list. { NodeType* location = listPtr; NodeType* tempLocation; if (IsThere(item) == true) { tempLocation = location->next; listPtr = listPtr->next; } else { while (IsThere(item) == true) { location = location->next; } tempLocation = location->next; location->next = (location->next)->next; } delete tempLocation; } //*********************************************************** void List::Print() const // Post: Items on the list are printed on the screen. { NodeType* location = listPtr; while (location != NULL) { cout << location->item << endl; location = location->next; } } //*********************************************************** int List::Length() const // Post: Number of items have been counted; result returned. { NodeType* location = listPtr; int length = 0; while (location != NULL) { location = location->next; length++; } return length; } //*********************************************************** List::~List() // Post: All the components are deleted. { NodeType* tempPtr; while (listPtr != NULL) { tempPtr = listPtr; listPtr = listPtr->next; delete tempPtr; } } int main () { ifstream InData; InData.open ("int.dat"); while (InData) { List.Insert(InData); } int length = List.Length(); cout << "There are " << length << " many items in this list." << endl; List.Print(); system ("pause"); return 0; }
Last edited by DemonGal711; Oct 8th, 2008 at 1:49 pm.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
why are you passing an argument of type ifstream to List.insert() when it obviously expects an argument of the type "itemType" ?
I understand you have your items in a file, but do you expect the file to read itself ? You need to write some code to read the contents of the file and then pass the correct arguments to the insert function.
I understand you have your items in a file, but do you expect the file to read itself ? You need to write some code to read the contents of the file and then pass the correct arguments to the insert function.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
ok I am not sure what your itemType is so lets assume its a basic data type like int or string and your file has those in it.
so
so
C++ Syntax (Toggle Plain Text)
itemType element; ifstream InData; InData.open ("int.dat"); if (inData.isopen()){ while (InData >> element){ List.Insert(element); } }
Last edited by stilllearning; Oct 8th, 2008 at 3:22 pm.
![]() |
Similar Threads
- Help. Parse error in PHP code (PHP)
- PHP Parse error: parse error, unexpected T_STRING (PHP)
- Parse error: parse error, unexpected T_STRING in /home/thei2k9/public_html/includes/f (PHP)
- Need Help With Parse Error... (PHP)
- Parse Error message when testing page on browser - HELP!! (PHP)
- Parse error: (PHP)
- I need help with a parse error! (PHP)
- Parse error, syntax error, Forbids declaration (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with AddFontResource
- Next Thread: tail recursion
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





