Reputation: 1
I'm getting errors trying to compile my prog program in my header file and I cant figure them out.
This is a simple linked list program to user input a list of integers and display them back. I will appreciate all the help.
Heres is my header file code
#ifndef Linklist
#define Linklist
#include<cstdlib>
class linked_list {
public:
linked_list() {head = NULL; tail = NULL;}
void insert_front (int num);
bool empty() {return (head == NULL);}
private:
node *head;
node *tail;
};
Below are the errors im getting
1>Compiling...
1>Linklist.cpp
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(19) : error C2143: syntax error : missing ';' before '*'
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(20) : error C2143: syntax error : missing ';' before '*'
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(12) : error C2065: 'head' : undeclared identifier
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(12) : error C2065: 'tail' : undeclared identifier
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(15) : error C2065: 'head' : undeclared identifier
1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(27) : fatal error C1070: mismatched #if/#endif pair in file 'c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h'
1>Build log was saved at "file://c:\Users\albert\Documents\Visual Studio 2008\Projects\Linklist\Linklist\Debug\BuildLog.htm"
1>Linklist - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Here is my program implementation
#include "linklist.h"
#include <iostream>
using namespace std;
int main()
{
void linked_list::insert_front (int num) {
node *head;
node *tail;
node *p;
int num;
head = NULL;
tail = NULL;
node *p = new node;
p->set_data ( num):
p->set_next ( head);
head = p;
for (int i=0; i<3; i++)
{
cout << "Enter number :";
cin >> num;
newNode = new nodeType; // Create the new node
newNode->data = num; // and assign its data value
newNode->link = NULL; // make its link point to nothing
if (first == NULL) // If there is nothing in the list, then make the
{
first = newNode; // newNode the first item and the last item
last = newNode;
}
else // Else if first already has a value
{
last->link = newNode; // make the last item link to the newNode
last = newNode; // and make newNode the last item
}
}
// Display the list
DisplayList(first);
system("PAUSE");
return(0);
}
Upvotes: 0
Views: 2054
Reputation: 14434
The type node
is not in scope.
You probably need a new header file node.h to define the missing type, if the type will elsewhere be used. Otherwise, you should at least define it at the top of the file you have listed.
Of greater concern is that you do not understand the importance of proper indentation of code. Until you do, problems like this will plague you.
Upvotes: 0
Reputation: 16148
If this isn't some sort of homework assignment then please consider using std::list
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
std::cout << "Enter ints end with q" << std::endl;
std::list<int> l; //a deque is probably better TBH
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::inserter<int>(l, l.begin()));
std::copy(l.begin(), l.end(),
std::ostream_iterator<int>(std::cout, " "));
}
Rewriting existing containers is just a bad idea; also in this use case a deque is better than a list.
Upvotes: 1
Reputation: 6487
I never worked on VC++... but try declaring the private section in the header file before the public section... And is node a built in data type in VC++?
Upvotes: 0
Reputation: 409136
Do you define the node
class anywhere? You need to include the file it's in in the header file as well, or at least define it:
class node;
class linked_list { ... };
Also, don't forget the #endif
at the end of the header file!
Upvotes: 1
Reputation: 224844
You're using some type node
all over the place, but it isn't defined anywhere in your program. You're going to need to do that.
In addition there is some funny business going on with nested functions in main()
. I don't know if you meant to do that or not, but it's certainly strange.
Upvotes: 2