Hi,

I'm trying to construct my first Doubly Linked List, but am having trouble compiling.

This is my program:

main.cpp :

#include <iostream>
#include "node.h"
#include "doublylinkedlist.h"
using namespace std;

int main()
{
	system ("PAUSE");
	return 0;
}

node.h :

class Node
{
public:
	Node *pointertonextnode;//pointer to the next node of type node
	Node *pointertopreviousnode;//pointer to the previous node of type node

	int nodedatamember;//a node's data memeber
};

doublylinkedlist.h :

class doublylinkedlist
{
public:

	Node *pointertofrontoflist;//pointer to front of list
	Node *pointertobackoflist;//pointer to back of list  

	//constructor to contruct a blank doubly linked list:
	doublylinkedlist()
	{
		pointertofrontoflist = NULL;
		pointertobackoflist = NULL;
	}

	void insertBeginningWhenListIsEmpty(doublylinkedlist List, Node newNode);
};

doublylinkedlist.cpp :

#include "doublylinkedlist.h"

void doublylinkedlist::insertBeginningWhenListIsEmpty(doublylinkedlist List, Node newNode)
{
	if(List.pointertofrontoflist == NULL)
	{
		List.pointertofrontoflist = newNode;
		List.pointertobackoflist = newNode;
		newNode.pointertonextnode = NULL;
		newNode.pointertopreviousnode = NULL;
	}
	else
	{
		cout << "Create a Insert before function";
		//insert before function
	}
}

In Visual Studio, the first compile error reports:

error C2143: syntax error : missing ';' before '*'

Which is the line in bold within the doublylinkedlist.h file.

I can't see the error which would cause this, and so am stumped.

Could anyone point my error?

Many thanks for any help with this!


//-------------------------------------------------
(Full error list if that will help):

error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2061: syntax error : identifier 'Node'
error C2065: 'pointertofrontoflist' : undeclared identifier
error C2065: 'NULL' : undeclared identifier
error C2065: 'pointertobackoflist' : undeclared identifier
error C2061: syntax error : identifier 'Node'
error C2039: 'pointertofrontoflist' : is not a member of 'doublylinkedlist'
see declaration of 'doublylinkedlist'
error C2039: 'pointertofrontoflist' : is not a member of 'doublylinkedlist'
see declaration of 'doublylinkedlist'
error C2065: 'newNode' : undeclared identifier
error C2039: 'pointertobackoflist' : is not a member of 'doublylinkedlist'
see declaration of 'doublylinkedlist'
error C2228: left of '.pointertonextnode' must have class/struct/union

Edited 6 Years Ago by Carrots: n/a

does your class doublylinkedlist know what a class Node is? if not, trying to create a pointer to one will surely result in an error.

EDIT: I tested it and that wasn't the problem.
I used the Visual Studio pre-compiled header, fixed the lines 7&8 in doublylinkedlist.cpp ( &newNode ) and it compiled fine.

Edited 6 Years Ago by Topi Ojala: n/a

Comments
Thanks buddy :)

Thanks for helping Topi, but I'm still having problems.

Did I make a mistake with what you suggested?

main.cpp:

#include <iostream>
#include "node.h"
#include "doublylinkedlist.h"
using namespace std;

int main()
{
	system ("PAUSE");
	return 0;
}

node.h:

#ifndef NODE.H
#define NODE.H

class Node
{
public:
	Node *pointertonextnode;//pointer to the next node of type node
	Node *pointertopreviousnode;//pointer to the previous node of type node

	int nodedatamember;//a node's data memeber
};

#endif

doublylinkedlist.h:

#ifndef DOUBLYLINKEDLIST.H
#define DOUBLYLINKEDLIST

#include "node.h"
class doublylinkedlist
{
public:

	Node *pointertofrontoflist;//pointer to front of list   
	Node *pointertobackoflist;//pointer to back of list  

	//constructor to contruct a blank doubly linked list:
	doublylinkedlist()
	{
		pointertofrontoflist = null;
		pointertobackoflist = null;
	}

	void insertBeginningWhenListIsEmpty(doublylinkedlist List, Node newNode);
};

#endif

doublylinkedlist.cpp:

#include "doublylinkedlist.h"
#include "node.h"
void doublylinkedlist::insertBeginningWhenListIsEmpty(doublylinkedlist List, Node newNode)
{
	if(List.pointertofrontoflist == NULL)
	{
		List.pointertofrontoflist = &newNode;////////////////////
		List.pointertobackoflist = &newNode;////////////////////
		newNode.pointertonextnode = NULL;
		newNode.pointertopreviousnode = NULL;
	}
	else
	{
		cout << "Create a Insert before function";
		//insert before fucntion
	}
}

Visual Studio says:

unexpected tokens following preprocessor directive - expected a newline

Really appreciate any help with this :)

Thanks!

should the "name" given in the preprocessor command match on lines 1 and 2 in doublylinkedlist.h ?

other than that I'm not sure what the problem might be ( or you might have figured it out by now :)

Topi is on the right track I think. A portion of it has to do with your preprocessor directives. I've never seen one with a period in it and this is why I suppose. Most people use an underscore in place of the period (the convention is to name it after your header like you had done, but really it could be anything). So change those to NODE_H, DOUBLYLINKEDLIST_H etc.

NULL is something that is defined in a couple of the headers so it has to be in all caps. You need to include <iostream> for the NULL and the couts. Also you need to qualify cout one of 3 ways: either put std:: in front of it, std::cout, or put using std::cout; at the top of your file, or the last option put using namespace std; at the top of your file (which is the least favorable since you have to worry about all the names in that namespace conflicting with the code you've written.

This question has already been answered. Start a new discussion instead.