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

Dani AI

Generated

A few concrete problems explain the compiler errors seen in the thread. The C2143 "missing ';' before '*'" is a common symptom that the compiler didn't know the type Node at the point it parsed the pointer declaration in the list header — the preprocessor/headers were broken so Node never reached the compiler. The edits in Post #3 introduced invalid preprocessor tokens (macro names with a dot) and a mismatched #ifndef/#define, and null (lowercase) is not a valid null-pointer constant. 's note about precompiled headers is also relevant for Visual Studio: if precompiled headers are enabled they must be included first in every .cpp.

Concrete fixes and safer design choices:

  • Use valid include guards or #pragma once. Macro names must be identifiers (letters/digits/underscore). Example guard form:

    #ifndef DOUBLYLINKEDLIST_H
    #define DOUBLYLINKEDLIST_H
    
    class Node;            // forward declaration when only pointers are stored
    
    class doublylinkedlist {
        Node* head;
        Node* tail;
    public:
        doublylinkedlist() : head(nullptr), tail(nullptr) {}
        void insertAtFront(Node* newNode);   // take a pointer, or provide an insert(data) that allocates internally
    };
    
    #endif
  • Prefer nullptr in modern C++ (or NULL with <cstddef> if compiling pre-C++11). Do not write null.

  • Do not take Node by value and then store &newNode — that stores the address of a local/copy and becomes a dangling pointer. Either allocate (new) and store the returned Node*, or make the insert method allocate the node from supplied data.

  • Member functions should mutate this; removing the redundant doublylinkedlist List parameter avoids copying the list.

Troubleshooting checklist (ties to earlier posts): fix the guard names as suggested, include the precompiled header first in .cpp if the project uses one, add <iostream> and use std::cout in .cpp, compile with warnings enabled to catch dangling/address-of-local issues, and recompile after each small change to isolate which fix resolves the original C2143. ’s original errors will be resolved once Node is made visible and the insert API uses pointers/references instead of taking addresses of temporaries.

Recommended Answers

All 4 Replies

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.

commented: Thanks buddy :) +1

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.