Linked List Compile Error

Doublly Linked LIst using Templated Classes and Overidden Operators

Page 1 of 1

4 Replies - 1418 Views - Last Post: 14 April 2009 - 01:20 PM Rate Topic: -----

#1 Vesper103  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-April 09

Linked List Compile Error

Posted 12 April 2009 - 07:46 PM

So I wrote a Doubly linked list Using templated classes and Operators. I got it all boiled down to 1 error:

Quote

c:\documents and settings\.......\projects\linkedlist\linkedlist\griffon.h(172) : error C2059: syntax error : ';'
c:\documents and settings\.......\projects\linkedlist\linkedlist\griffon.h(170) : while compiling class template member function 'const griffon<T> &griffon<T>::operator =(T)'
with
[
T=int
]
c:\documents and settings\........\projects\linkedlist\linkedlist\main.cpp(7) : see reference to class template instantiation 'griffon<T>' being compiled
with
[
T=int
]



I looked through it but I cant see what it is. and Surrounded the erronius Code with comments... plz help

/*Project Name: Griffon
  Start Date: 4/10/2008
  Finish Date:
  File Size:
  Filename: "griffon.h"
  Purpose: To Create a Doublly LInked List utilizing Templated classes and 
		   Overloaded Operators.
*/

/*-------------------------------------------
Class: Data
---------------------------------------------*/
template <class T>
class data
{
public:
	T val;//holds the actual value of each node
	int id;//a refrence for the data
};

/*-------------------------------------------
Class: Node
---------------------------------------------*/
template <class T>
class node
{
public:
	data<T> d; //stores the data in a more organized node
	node* next;//points to the next node in the list
	node* last;//points to the last node in the list
};

/*-------------------------------------------
Class: Griffon
---------------------------------------------*/
template <class T>
class griffon
{
public:
	griffon();//default constructor
	~griffon();//default destructor
	int move(int id);//moves to specified id
	const griffon& operator= (T val);
	void add();
private:
	node<T>* n;//the currently pointed to node
	node<T>* root;//the fist node to hold placement
	int current_id;//holds the current data_id being used
	int next();//moves forward in list, returns if successful
	int last();//moves backward in list, returns if successful
	int set(T val);//sets current node equal to Val
	void ne();
	//void del();//deletes current node sliding all next values back one and adjusting id's
	//void plug();//adds a new blank node sliding all values up and adjusting id's
};

/*-------------------------------------------
Functions Class: Griffon
---------------------------------------------*/

//constructor
template <class T>
griffon<T>::griffon()
{
	root=new node<T>;//makes a starting node
	n=root;//sets pointer to point to the root
	root->next=0;//sets next and blank to be null
	root->last=0;
	root->d.id=0;//sets id to 0 so value is saved
	current_id=1;//sets next id for easy use
}

//Destructor
template <class T>
griffon<T>::~griffon()
{
	while(n->next!=0)//deletes all nodes
	{
		n=n->next;
		delete root;
		root=n;
	}
	n=0;//sets both pointers to null to avoid crashes
	root=0;
	current_id=0;
}

//next
template <class T>
int griffon<T>::next()
{
	if(n->next!=0)
	{
		n=n->next;
		return 1;
	}
	else
	{
		return 0;
	}
}

//last
template <class T>
int griffon<T>::last()
{
	if(n!=root)
	{
		n=n->last;
		return 1;
	}
	else
	{
		return 0;
	}
}

//set
template <class T>
int griffon<T>::set(T val)
{
	if(n!=root)
	{
		n->d.val=val;
		return 1;
	}
	else
	{
		return 0;
	}
}

//move
template <class T>
int griffon<T>::move(int id)
{
	if(id<=current_id)//checks to see if requested id is in scope
	{
		if(id>n->d.id)//checks to see if it shhould move backwards
		{
			while(id!=n->d.id)//loops backwards until it reaches target
			{
				last();//calls last function
			}
		}
		else
		{
			if(id<n->d.id)//cehcks to see if it should move up
			{
				while(id!=n->d.id)//loops until target is reached
				{
					next();//moves
				}
			}
			else
			{
				return 1;//returns success
			}
		}
	}
	else
	{
		return 0;//failed
	}
}








/////////////////////////////////////////////////////////////////////////////////////////
//ERROR HERE
//set operator
template <class T>
const griffon<T>& griffon<T>::operator= (T val)
{
	n->d.val=val;
	return this*;
}
//TO HERE
/////////////////////////////////////////////////////////////////////////////////////////






//new
template <class T>
void griffon<T>::ne()
{
	n->next=new node<T>;
	n->next->last=n;
	n=n->next;
	n->next=0;
}
//add
template <class T>
void griffon<T>::add()
{
	ne();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Compile Error

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Linked List Compile Error

Posted 12 April 2009 - 08:09 PM

Don't you mean
return *this;
???
Was This Post Helpful? 0
  • +
  • -

#3 Vesper103  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-April 09

Re: Linked List Compile Error

Posted 13 April 2009 - 08:19 PM

Thanks man that fixed it now it compiles and runs but I have the same problem i always do when I try this and I can never figure out why. When I compile it i get values of like -823674100 and stuff for my values and the id for each node. If i dont use templated lists i have no problem. again here is src:


Griffon.h
/*Project Name: Griffon
  Start Date: 4/10/2008
  Finish Date:
  File Size:
  Filename: "griffon.h"
  Purpose: To Create a Doublly LInked List utilizing Templated classes and 
		   Overloaded Operators.
*/

/*-------------------------------------------
Class: Data
---------------------------------------------*/
template <class T>
class data
{
public:
	T val;//holds the actual value of each node
	int id;//a refrence for the data
};

/*-------------------------------------------
Class: Node
---------------------------------------------*/
template <class T>
class node
{
public:
	data<T> d; //stores the data in a more organized node
	node* next;//points to the next node in the list
	node* last;//points to the last node in the list
};

/*-------------------------------------------
Class: Griffon
---------------------------------------------*/
template <class T>
class griffon
{
public:
	griffon();//default constructor
	~griffon();//default destructor
	int move(int id);//moves to specified id
	const griffon& operator= (T val);
	T ret();
	void add();
private:
	node<T>* n;//the currently pointed to node
	node<T>* root;//the fist node to hold placement
	int current_id;//holds the current data_id being used
	int next();//moves forward in list, returns if successful
	int last();//moves backward in list, returns if successful
	int set(T val);//sets current node equal to Val
	void ne();
	//void del();//deletes current node sliding all next values back one and adjusting id's
	//void plug();//adds a new blank node sliding all values up and adjusting id's
};

/*-------------------------------------------
Functions Class: Griffon
---------------------------------------------*/

//constructor
template <class T>
griffon<T>::griffon()
{
	root=new node<T>;//makes a starting node
	n=root;//sets pointer to point to the root
	root->next=0;//sets next and blank to be null
	root->last=0;
	root->d.id=0;//sets id to 0 so value is saved
	current_id=1;//sets next id for easy use
}

//Destructor
template <class T>
griffon<T>::~griffon()
{
	while(n->next!=0)//deletes all nodes
	{
		n=n->next;
		delete root;
		root=n;
	}
	n=0;//sets both pointers to null to avoid crashes
	root=0;
	current_id=0;
}

//next
template <class T>
int griffon<T>::next()
{
	if(n->next!=0)
	{
		n=n->next;
		return 1;
	}
	else
	{
		return 0;
	}
}

//last
template <class T>
int griffon<T>::last()
{
	if(n!=root)
	{
		n=n->last;
		return 1;
	}
	else
	{
		return 0;
	}
}

//set
template <class T>
int griffon<T>::set(T val)
{
	if(n!=root)
	{
		n->d.val=val;
		return 1;
	}
	else
	{
		return 0;
	}
}

//move
template <class T>
int griffon<T>::move(int id)
{
	if(id<=current_id)//checks to see if requested id is in scope
	{
		if(id>n->d.id)//checks to see if it shhould move backwards
		{
			while(id!=n->d.id)//loops backwards until it reaches target
			{
				last();//calls last function
			}
		}
		else
		{
			if(id<n->d.id)//cehcks to see if it should move up
			{
				while(id!=n->d.id)//loops until target is reached
				{
					next();//moves
				}
			}
			else
			{
				return 1;//returns success
			}
		}
	}
	else
	{
		return 0;//failed
	}
}

//set operator
template <class T>
const griffon<T>& griffon<T>::operator= (T val)
{
	n->d.val=val;
	return *this;
}


//new
template <class T>
void griffon<T>::ne()
{
	n->next=new node<T>;
	n->next->last=n;
	n=n->next;
	n->next=0;
}
//add
template <class T>
void griffon<T>::add()
{
	ne();
}

template <class T>
T griffon<T>::ret()
{
	return n->d.val;
}



Main.cpp
#include <iostream>
using namespace std;
#include "griffon.h"

int main()
{
	griffon<int> g;
	int x=0;
	while(x<=1000)
	{
		g.add();
		g=x;
		x++;
	}
	x=0;
	g.move(0);
	while(x<=1000)
	{
		cout<<x<<": "<<g.ret();
		g.move(x);
		x++;
	}
	while(1){}
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 tootintorrey  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-March 08

Re: Linked List Compile Error

Posted 14 April 2009 - 12:39 PM

i found that usually when i get a number way out of the range of anything im working its because something its pointing to the wrong address (either a non existent one or just the wrong one) in your case seems like a non existent. sorry i cant find out what or why in your code
Was This Post Helpful? 0
  • +
  • -

#5 Vesper103  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 01-April 09

Re: Linked List Compile Error

Posted 14 April 2009 - 01:20 PM

Okay, Ill look at my pointers then. Thanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1