LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-14-2004, 05:19 PM   #1
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
C++ syntax error before :: token HELP, i cant find the syntax error :(


i have written a template class that acts as a lightning fast linked list.
unlike basic linked lists, it does not slow down as the size of the list gets bigger.
and the template nature of it allows it to be an array of any datatype or object.

i have an unusual syntax error reported by the compiler, and i cant find whate wrong.

i compile with the GNU c++ compiler with the command like "g++ -o tester.bin tllisttest.cpp" for anyone who want to try compileing with GCC, but i would welcome any error
messages reported by other compilers.. this source code SHOULD also compile on microsofts visual C++ under the windows platform, although ive not been able to test that.

PLEASE help, this is driving me mad :(

here is the compiler error output.

Quote:
bash-2.05b$ g++ -o test.bin tllisttest.cpp
In file included from tllisttest.cpp:6:
tllist.h:55: error: syntax error before `::' token
tllist.h:55: error: invalid use of template-name 'cllist' in a declarator
tllist.h:55: error: syntax error before `{' token
tllist.h:56: error: syntax error before `::' token
tllist.h:56: error: invalid use of template-name 'cllist' in a declarator
tllist.h:56: error: syntax error before `{' token
tllist.h:57: error: syntax error before `::' token
tllist.h:57: error: type specifier omitted for parameter `dataType'
tllist.h:57: error: parse error before `)' token
tllist.h:57: error: invalid use of template-name 'cllist' in a declarator
tllist.h:57: error: syntax error before `{' token
tllist.h:58: error: syntax error before `::' token
tllist.h:58: confused by earlier errors, bailing out
here are lines 55 to 58 of tllist.h
Code:
template<class dataType> cllist::cllist() { init();}
template<class dataType> cllist::cllist(unsigned long n) { init(n);}
template<class dataType> cllist::cllist(unsigned long n, dataType defVal){ init(n,defVal);}
template<class dataType> cllist::~cllist(){ flush();}
you will notice there is no return type, ive checked the manual, and i know constructors dont have return types, (not even void's) i tried adding void, same error message.

this error message makes no sence !!!!

here is the cokmplete source code of tllist.h
Code:
// template linked list class by chris stones.
// a class to hold a variable array / stack of any type.
// maximum size of array = 4,294,967,296 (unsigned long).
// the Arrays performance does NOT decreace with size when used in a sequential mannor. 

// a single link in the list
template <class dataType>
struct clink {
	clink<dataType> *left;
	clink<dataType> *right;
	dataType	 data;
};

// floater. a marker that points to the item in the list that was last accessed in the hope that the next
// link that needs finding will probably lie next to it.
template <class dataType>
struct cfloater {
	unsigned long currentIndex;
	clink<dataType> *currentAddress;
};

// an exception to throw
class TLLIST_OUT_OF_BOUNDS{};

// where all the magick happens.
template <class dataType>
class cllist {
private:
	clink<dataType> first,last;
	cfloater<dataType> floater;
	unsigned long size;
	
	void init();
	void init(unsigned long, dataType);
	void init(unsigned long);
	
	clink<dataType> *access(unsigned long);

public:
	~cllist();
	cllist();					// default constructor, sets up an emptly list.
	cllist(unsigned long);				// single parameter constructor, sets up an array of the specified size.
	cllist(unsigned long,dataType);			// same as above, but with initiated to match parameter 2.
	
	dataType pop(unsigned long);			// remove a single item from anywhere in the list.
	dataType popLeft();				// remove the first (left) item from the fist.
	dataType popRight();				// remove the last (right) item from the list.
	
	void push(unsigned long, dataType);		// insert an item anywhere on the list.
	void pushLeft(dataType);			// add an item onto the beggining (Left) of the list
	void pushRight(dataType);			// add an item onto the end (right) of the list
	
	void flush();					// empty the list.
	unsigned long getSize();			// return the size of the list.
	
	dataType& operator [](double);			// overloaded [] for array like access to the list.
};

// constructors + destructors
template<class dataType> cllist::cllist() { init();}
template<class dataType> cllist::cllist(unsigned long n) { init(n);}
template<class dataType> cllist::cllist(unsigned long n, dataType defVal){ init(n,defVal);}
template<class dataType> cllist::~cllist(){ flush();}

// smaller function boddies
template<class dataType> void cllist::init(unsigned long n, dataType defVal) { while(size != n) { push(defVal);}}
template<class dataType> void cllist::init(unsigned long n) { init(); init(n,dataType());}
template<class dataType> dataType cllist::popLeft() { return pop(0);}
template<class dataType> dataType cllist::popRight() { return pop(size-1);}
template<class dataType> void cllist::pushLeft(dataType data) { push(0,data);}
template<class dataType> void cllist::pushRight(dataType data) { push(size, data);}
template<class dataType> dataType& cllist::operator [](double n) { return access(n)->data;}
template<class dataType> void cllist::flush() { while(size != 0){ pop(0);}}
template<class dataType> unsigned long cllist::getSize() { return size;}

template<class dataType> void cllist::init(){
	// basic initilisation
	size			= 0;
	first.left		= 0;
	first.right		= &last;
	last.right		= 0;
	last.left		= &first;
	floater.currentIndex	= 0;
	floater.currentAddress	= 0;
}

template<class dataType> clink<dataType>* cllist::access(unsigned long index) {
			
	// a quick check...
	if(index >= size) { throw TLLIST_OUT_OF_BOUNDS(); }
		
	// which is closer to the desired link ? (first, last or floater)
	unsigned long distanceFirst   = index + 1;
	unsigned long distanceLast    = (size + 1) - index;
	unsigned long distanceFloater;
		
	// the first time access() is called, floater is not inside the actual array,
	// and because index is an unsigned variable (not able to be -1) its index is incorrect to its position,
	// ignore floater the first time the closest point is calculated.
	if (floater.currentAddress != &first) {
		if (index >= floater.currentIndex) { distanceFloater = index - floater.currentIndex; }
		else				   { distanceFloater = floater.currentIndex - index; }
	}
	else { distanceFloater = size; } // make it impossibly big, so one of the others is selected as closer.
		
	// is the required index point to the right, or left of the closest known index point ?
	bool loopRight = true;
		
	// default, first is closest
	clink<dataType> *thisLink = &first;
	unsigned long distance = distanceFirst;
		
	// maybe last is closer ??
	if (distanceLast    < distance) { 
		distance = distanceLast; 
		thisLink = &last; 
		loopRight = false;
	}
		
	// or maybe the floater ??
	if (distanceFloater < distance) { 
		distance = distanceFloater; 
		thisLink = floater.currentAddress; 
		if (floater.currentIndex > index) { loopRight = false; }
		else				  { loopRight = true; }	
	}
		
	// now loop to the required access index.
	while(distance != 0) {
		distance--;
		if (loopRight) { thisLink = thisLink->right; }
		else	       { thisLink = thisLink->left;  }
	}
		
	// set floater to this link UNLESS accessing the first of last link (remembed by first and last local variable)
	if(index != 0 && index != size-1) {
		floater.currentIndex = index;
		floater.currentAddress = thisLink;
	}
		
	//return result
	return thisLink;
}

template<class dataType> dataType cllist::pop(unsigned long n) {
	
	clink<dataType> *popLink = access(n);
	dataType forReturn = popLink->data;
		
	// correct pointers on either side of the link to be poped
	popLink->left->right = popLink->right;
	popLink->right->left = popLink->left;
		
	// correct size parameter
	size--;
		
	// are we about to delete the object that the floater points to ?
	if(popLink == floater.currentAddress) {
		// move the floater to the right, (index remains correct)
		floater.currentAddress = popLink->right;
	}
	else {
		// the object floater points to will not be deleted.
		// however, if we deleted an object on the left of floater, then floaters
		// index needs to be decremented.
		if(n < floater.currentIndex) { floater.currentIndex--; }
	}
	// delete the poped link
	delete popLink;
		
	// return the data
	return forReturn;
}

template<class dataType> void cllist::push(unsigned long index, dataType value) {
	
	// to push to position n is to add a link with the index of n. pushing the last index n to index n+1.
	clink<dataType> *rightLink;
	clink<dataType> *leftLink;
	
	// create a new link.
	clink<dataType> *newLink = new clink<dataType>;
	newLink->data = value;
		
	// get the link which will be to the right of the new link
	// special case.. PUSHING to the very end of the list. will throw an out of bounds if we call accessLink()
	if (index == size) { rightLink = &last; }
	else               { rightLink = access(index); }
		
	// get the link which will be to the left of the new link
	leftLink = rightLink->left;
		
	// insert the new link
	newLink->right = rightLink;
	newLink->left  = leftLink;
	rightLink->left = newLink;
	leftLink->right = newLink;
		
	// fix the size variable
	size++;
		
	// fix the floater....
	// dont move the floater if we are pushing to the very right, or left of the list
	if(index != size-1 && index != 0) {
		floater.currentIndex = index;
		floater.currentAddress = newLink;
	}
	else {
		// if we just pushed to the left of the floater, then floaters index needs to be incrememnted
		if(index <= floater.currentIndex) {
			floater.currentIndex++;
		}
	}
}

and HERE is a program i wrote to test the tllist class
Code:
// linked list tester by chris stones.

#include<iostream>
using namespace std;

#include"tllist.h"

// tests...
//	add 100 items to the list
//	remove link from random place
//	add link to random place
//	output entire list (with array interface)
//	output entire list by popping.

// exception tests...
//	Out of bounds

int main() {

	cllist<int> stack;
	int n;
	
	cout << "pushing 20 numbers to the right" << endl;
	for (n=0; n<20; n++) { stack.pushRight(n); }
	
	cout << "output entire list" << endl;
	for (n=0; n<20; n++) { cout << stack[n] << '|'; }
	
	cout << "output intex 10" << endl;
	cout << stack[10] << endl;
	
	cout << "output index 15" << endl;
	cout << stack[15] << endl;
	
	cout << "insert 99 into index 10" << endl;
	stack.push(10,99);
	
	cout << "remove index 1 = ";
	cout << stack.pop(1) << endl;
	
	cout << "pop all items off list" << endl;
	while(stack.getSize != 0) {
		cout << stack.popLeft() << '|';
	}
	
	cout << "cause out of bounds error" << endl;
	stack.popRight();

	return 0;
}
 
Old 12-14-2004, 05:33 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
You need to put <dataType> after the class name, like so:

Code:
template<class dataType> cllist<dataType>::cllist() { init();}
Kind of redundant, I know, but I'm sure there's a logical reason for it. Anyhow, use that for all your function definitions and you should be OK.

Last edited by wapcaplet; 12-14-2004 at 05:34 PM.
 
Old 12-14-2004, 06:09 PM   #3
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Original Poster
Rep: Reputation: 47
Lol... i knew that !!!
caffine is no substitute to sleep.

thanks ALOT !
god knows how many hours of extra sleep you just bought me !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error near unexpected token `fi' Warmduvet Programming 21 10-08-2013 10:28 AM
syntax error in C ++ mshinska Programming 3 10-07-2005 04:53 PM
Syntax error phernand Programming 1 05-10-2005 12:04 PM
Many errors when 'make'ing (example: error: syntax error before `::' token) darkblade Linux - Software 5 03-02-2005 03:00 PM
ERROR running make when installing Nvidia drivers (syntax error) randyriver10 Linux - Software 5 02-21-2004 04:51 PM


All times are GMT -5. The time now is 05:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration