LinuxQuestions.org
Visit Jeremy's Blog.
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 05-12-2004, 06:03 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Rep: Reputation: 47
C++ templates + linked lists... lost.. very lost..


I do not even know how to start debugging this, from the example I have I cannopt see a problem, and the error messages might as well just say hey there was an error go fetch, oh and good luck were as cluseless as you are.

code:
Code:
#include <iostream>
#include <string>
#include <cstddef>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

template<class T>
struct Node
{
	string Name;
	T data;
	Node *Next;
};

typedef Node* NodePtr;

template<class T>
void Insert(NodePtr after, T MyStuff);

template<class T>
void Insert(NodePtr Head, string AfterName, T MyStuff, string MyName);

template<class T>
void Insert(NodePtr Head, int AfterIDX, T MyStuff, string MyName);

template<class T>
void Insert(NodePtr Head&, T MyStuff, string MyName);

template<class T>
T Get(NodePtr Head, string name);

template<class T>
T Get(NodePtr Head, int IDX);

int main()
{
	cout << "bleh!";
	return 0;
}

template<class T>
void Insert(NodePtr after, T MyStuff, string MyName) //initially I copied this structure from 15-13, not an exact copy, but a copy of the idea.
{
	NodePtr temp; //declare a new node
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	temp->Next = after->Next; // Connect the nodes after the new one to the new one
	after->Next = temp; //connect the new node to the ones that came before it
}

template<class T>
void Insert(NodePtr Head, string AfterName, T MyStuff, string MyName)
{
	NodePtr temp; //declare a new node
	NodePtr after; //make an empty pointer that will contain a reference to the needed node of the linked list.
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	after = Get(Head, AfterName); //get the node that should be in here
	temp->Next = after->Next; // Connect the nodes after the new one to the new one
	after->Next = temp; //connect the new node to the ones that came before it
}

template<class T>
void Insert(NodePtr Head, int AfterIDX, T MyStuff, string MyName)
{
	NodePtr temp; //declare a new node
	NodePtr after; //make an empty pointer that will contain a reference to the needed node of the linked list.
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	after = Get(Head, AfterIDX); //get the node that should be in here
	temp->Next = after->Next; // Connect the nodes after the new one to the new one
	after->Next = temp; //connect the new node to the ones that came before it
}

template<class T>
void Insert(NodePtr& Head, T MyStuff, string MyName)
{
	NodePtr temp; //declare a new node
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	temp->Next = Head; // Connect the nodes after the new one to the new one
	Head = temp; //connect the new node to the ones that came before it
}

template<class T>
T Get(NodePtr Head, string name)
{
	NodePtr temp;
	temp = Head;
	while (Head->Next != NULL)
	{
		if (Head->Name == name)
		{
			return temp;
		}
		temp = temp->next;
	}
	cout << "Error! no such name!";
	exit(1);
}

template<class T>
T Get(NodePtr Head, int IDX)
{
	NodePtr temp;
	temp = Head;
	int counter = 0;
	while (counter < IDX)
	{
		temp = temp->next;
		counter++;
		if (temp->next == NULL)
		{
			cout << "Error! no such name!";
			exit(1);
		}
	}
	return temp;
}
error:

assign5.cpp:18: syntax error before `*' token
assign5.cpp:21: syntax error before `,' token
assign5.cpp:24: syntax error before `,' token
assign5.cpp:27: syntax error before `,' token
assign5.cpp:30: syntax error before `&' token
assign5.cpp:33: syntax error before `,' token
assign5.cpp:36: syntax error before `,' token
assign5.cpp:45: syntax error before `,' token
assign5.cpp: In function `void Insert(...)':
assign5.cpp:47: syntax error before `;' token
assign5.cpp:48: syntax error before `;' token
assign5.cpp: At global scope:
assign5.cpp:56: syntax error before `,' token
assign5.cpp:57: redefinition of `template<class T> void Insert(...)'
assign5.cpp:46: `template<class T> void Insert(...)' previously declared here
assign5.cpp: In function `void Insert(...)':
assign5.cpp:58: syntax error before `;' token
assign5.cpp:60: syntax error before `;' token
assign5.cpp: At global scope:
assign5.cpp:69: syntax error before `,' token
assign5.cpp:70: redefinition of `template<class T> void Insert(...)'
assign5.cpp:57: `template<class T> void Insert(...)' previously declared here
assign5.cpp:70: redefinition of `template<class T> void Insert(...)'
assign5.cpp:46: `template<class T> void Insert(...)' previously declared here
assign5.cpp: In function `void Insert(...)':
assign5.cpp:71: syntax error before `;' token
assign5.cpp:73: syntax error before `;' token
assign5.cpp: At global scope:
assign5.cpp:82: syntax error before `,' token
assign5.cpp:83: redefinition of `template<class T> void Insert(...)'
assign5.cpp:70: `template<class T> void Insert(...)' previously declared here
assign5.cpp:83: redefinition of `template<class T> void Insert(...)'
assign5.cpp:57: `template<class T> void Insert(...)' previously declared here
assign5.cpp:83: redefinition of `template<class T> void Insert(...)'
assign5.cpp:46: `template<class T> void Insert(...)' previously declared here
assign5.cpp: In function `void Insert(...)':
assign5.cpp:84: syntax error before `;' token
assign5.cpp:85: syntax error before `;' token
assign5.cpp: At global scope:
assign5.cpp:93: syntax error before `,' token
assign5.cpp: In function `T Get(...)':
assign5.cpp:95: syntax error before `;' token
assign5.cpp: At global scope:
assign5.cpp:110: syntax error before `,' token
assign5.cpp:111: redefinition of `template<class T> T Get(...)'
assign5.cpp:94: `template<class T> T Get(...)' previously declared here
assign5.cpp: In function `T Get(...)':
assign5.cpp:112: syntax error before `;' token

Last edited by exodist; 05-12-2004 at 06:04 PM.
 
Old 05-12-2004, 08:52 PM   #2
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
Fixed that problem.

the problem was in typedef, can't use it.

however now I hit a new problem:

Code:
#include <iostream>
#include <string>
#include <cstddef>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

template<class T>
struct Node
{
	string Name;
	T data;
	Node *Next;
};

template<class T>
void Insert(Node<T>* after, T MyStuff);

template<class T>
void Insert(Node<T>* Head, string AfterName, T MyStuff, string MyName);

template<class T>
void Insert(Node<T>* Head, int AfterIDX, T MyStuff, string MyName);

template<class T>
T Get(Node<T>* Head, string name);

template<class T>
T Get(Node<T>* Head, int IDX);

int main()
{
	Node<double>* Test1 = new Node<double>;
	Test1->Name = "First";
	Test1->data = 45.56;

	double MyDouble = 22.22;

	string TestName("TwentyTwo dot TwentyTwo");

	Insert(Test1, MyDouble, TestName);

	cout << Test1->data;

	return 0;
}

template<class T>
void Insert(Node<T>* after, T MyStuff, string MyName) //initially I copied this structure from 15-13, not an exact copy, but a copy of the idea.
{
	Node<T>* temp; //declare a new node
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	temp->Next = after->Next; // Connect the nodes after the new one to the new one
	after->Next = temp; //connect the new node to the ones that came before it
}

template<class T>
void Insert(Node<T>* Head, string AfterName, T MyStuff, string MyName)
{
	Node<T>* temp; //declare a new node
	Node<T>* after; //make an empty pointer that will contain a reference to the needed node of the linked list.
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	after = Get(Head, AfterName); //get the node that should be in here
	temp->Next = after->Next; // Connect the nodes after the new one to the new one
	after->Next = temp; //connect the new node to the ones that came before it
}

template<class T>
void Insert(Node<T>* Head, int AfterIDX, T MyStuff, string MyName)
{
	Node<T>* temp; //declare a new node
	Node<T>* after; //make an empty pointer that will contain a reference to the needed node of the linked list.
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	after = Get(Head, AfterIDX); //get the node that should be in here
	temp->Next = after->Next; // Connect the nodes after the new one to the new one
	after->Next = temp; //connect the new node to the ones that came before it
}

/*
template<class T>
void Insert(Node<T>*& Head, T MyStuff, string MyName)
{
	Node<T>* temp; //declare a new node
	temp = new Node; //initilize the new node
	temp->data = MyStuff; // Give the new node its data
	temp->Name = MyName; // Give the new node its name
	temp->Next = Head; // Connect the nodes after the new one to the new one
	Head = temp; //connect the new node to the ones that came before it
}
*/

template<class T>
T Get(Node<T>* Head, string name)
{
	Node<T>* temp;
	temp = Head;
	while (Head->Next != NULL)
	{
		if (Head->Name == name)
		{
			return temp;
		}
		temp = temp->next;
	}
	cout << "Error! no such name!";
	exit(1);
}

template<class T>
T Get(Node<T>* Head, int IDX)
{
	Node<T>* temp;
	temp = Head;
	int counter = 0;
	while (counter < IDX)
	{
		temp = temp->next;
		counter++;
		if (temp->next == NULL)
		{
			cout << "Error! no such name!";
			exit(1);
		}
	}
	return temp;
}
error:

43: Could not find a match for 'Insert<T>(Node<double> *,double,string)' in function main()
48: 'MyDouble' is assigned a value that is never used in function main()
 
Old 05-12-2004, 11:04 PM   #3
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
It's quite easy!
There is no matching function predefinition (prototype). take a look at your prototypes:
Code:
template<class T>
void Insert(Node<T>* after, T MyStuff);

template<class T>
void Insert(Node<T>* Head, string AfterName, T MyStuff, string MyName);

template<class T>
void Insert(Node<T>* Head, int AfterIDX, T MyStuff, string MyName);
None of them has a header like this:
Code:
Insert(Test1, MyDouble, TestName);
with a (T*, T, string).
 
  


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
Linked Lists and Queues Kroenecker Programming 2 04-09-2005 01:59 AM
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
power lost from cable modem, interface lost a10392 Linux - Networking 4 11-16-2004 09:01 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration