On the function print() I get this error message below
Sep 19, 2019 at 8:51am UTC
I get this error when compiling my program:
1 2 3 4 5 6 7 8
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\newHeadInsert.cpp" -o "C:\Dev-Cpp\newHeadInsert.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Dev-Cpp\newHeadInsert.cpp: In member function `void linkedList::print()':
C:\Dev-Cpp\newHeadInsert.cpp:38: error: expected primary-expression before ' ;' token
Execution terminated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include<iostream>
#include<list>
#include<cassert>
using namespace std;
struct Node
{
int data;
Node *link;
};
typedef Node *NodePtr, *head, *tmp;
class linkedList
{
public :
void head_insert(NodePtr& head, int the_number);
//Precondition: The pointer variable head points to
//the head of a linked list.
//Postcondition: A new node containing the_number
//has been added at the head of the linked list.
bool empty() const ;
void print();
};
void linkedList::head_insert(NodePtr& head, int the_number)
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr->data = the_number;
temp_ptr->link = head;
head = temp_ptr;
}
void linkedList::print()
{
//Display the nodes
NodePtr tmp;
tmp = head;
while (tmp)
{
cout << tmp->data << " " <<endl;
tmp = tmp->link;
}
}
int main()
{
linkedList list;
NodePtr head, tmp, first;
int num;
list.head_insert(head, num);
list.print();
return 0;
}
Sep 19, 2019 at 9:20am UTC
Please explain this line of code. I can not parse it in my head.
typedef Node *NodePtr, *head, *tmp;
Sep 19, 2019 at 9:42am UTC
It is exactly like that in the text book.
Sep 19, 2019 at 9:54am UTC
But what does it mean? Is head a variable, or type or a pointer type?
Sep 19, 2019 at 10:34am UTC
Apart from that typedef, in print() line 38: head
is not a variable to assign.
Sep 19, 2019 at 5:40pm UTC
1 2 3 4 5
typedef Node *NodePtr, *head, *tmp;
//equivalent to
typedef Node *NodePtr;
typedef Node *head;
typedef Node *tmp;
they are all
alias of a Node pointer.
> It is exactly like that in the text book.
quite an horrendous typo.
and given that `list' does not encapsulate `Node', perhaps quite an horrendous book too.
Sep 26, 2019 at 10:58am UTC
Thank you to all.
Problem now solved and works fine
Topic archived. No new replies allowed.