LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ templates + linked lists... lost.. very lost.. (https://www.linuxquestions.org/questions/programming-9/c-templates-linked-lists-lost-very-lost-180848/)

exodist 05-12-2004 06:03 PM

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

exodist 05-12-2004 08:52 PM

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()

Mohsen 05-12-2004 11:04 PM

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).


All times are GMT -5. The time now is 11:51 AM.