C++ converting to template classes (operator errors and such)

EDIT: I fixed mostly everything and now I just have the following two errors in my AnyList.cpp file, though I'm not entirely sure if they're template related.

1. 'operator =' is ambiguous
2. unary '++': 'std:string' does not define this operator or a conversion to a type acceptable to the predefined operator.

So for these classes I'm supposed to convert the Node and AnyList classes (both in the AnyList files) to template classes. I've done what I know I can do so far.

AnyList.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef ANYLIST_H
#define ANYLIST_H

#include<iostream>
#include <string>           
using namespace std;

template<class T>
class Node
{
public:
	Node() : data(0), next(NULL) {}
	Node(const T& theData, Node *newNext) : data(theData), next(newNext){}
	Node* getNext() const { return next; }
	const T& getData( ) const { return data; }
    void setData(const T& theData) { data = theData; }
	void setNext(Node *newNext) { next = newNext; }
	~Node(){}
private:
    T data;     
    Node *next; //pointer that points to next node
};

template<typename T>
class AnyList
{   
    template<typename T>
    friend ostream& operator<<(ostream& out, const AnyList<T>& theList);

public:
    AnyList();  

    void insert(const T& elem);

    T getNumOfElem() const;

    void destroyList();

    ~AnyList();

private:
    Node<T> *first;
    T count;        
};

#endif 


AnyList.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "AnyList.h"

template<typename T>
ostream& operator<<(ostream& out, const AnyList<T>& theList)
{
	if (theList.first == NULL)
		out << "List is empty." << endl;
	else
	{
		Node<T> *temp = theList.first;

		while (temp != NULL)
		{
			out << temp->getData() << " ";
			temp = temp->getNext();
		}
	}

	return out;
}

template<typename T>
AnyList<T>::AnyList()
{
	first = NULL;
	count = 0;
}

template<typename T>
void AnyList<T>::insert(const T& elem)
{	
	Node<T> *newNode = new Node<T>(elem, nullptr);

	if (first == nullptr)
		first = newNode;
	else
	{
		Node<T> *temp = first;
		while (temp->getNext() != nullptr)
			temp = temp->getNext();
		temp->setNext(newNode);
	}

	++count;
}

template<typename T>
T AnyList<T>::getNumOfElem() const
{
	return count;
}

template<typename T>
void AnyList<T>::destroyList()
{ 
	Node<T>  *temp = first;
	
	while (temp != NULL)
    {
		first = first->getNext();
        delete temp;
		temp = first;
    }

	count = 0;
}

template<typename T>
AnyList<T>::~AnyList()
{
	destroyList();
}


Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "AnyList.h"

#include <iostream>
using namespace std;

void testInt();
void testDouble();
void testString();

int main()
{
    testInt();
    cout << endl;
    testDouble();
    cout << endl;
    testString();
    cout << endl;
    testPair();
    cout << endl;

    cout << endl;
    system("Pause");
    return 0;
}

void testInt()
{
    AnyList<int> list1, list2, list3;

    list1.insert(10);
    list1.insert(11);
    list1.insert(12);
    list1.insert(13);
    list1.insert(14);

    list2.insert(5);
    list2.insert(3);
    list2.insert(9);

    list3.insert(78);

    cout << "TEST: Ostream operator <<\n\n";
    cout << "\tList1 is: " << list1 << endl;
    cout << "\tList2 is: " << list2 << endl;
    cout << "\tList3 is: " << list3 << endl;

}

void testDouble()
{
    AnyList<double> list1, list2, list3;

    list1.insert(10.9);
    list1.insert(11.4);
    list1.insert(12.3);
    list1.insert(13.5);
    list1.insert(14.1);

    list2.insert(5.8);
    list2.insert(3.7);
    list2.insert(9.4);

    list3.insert(78.2);

    cout << "TEST: Ostream operator <<\n\n";
    cout << "\tList1 is: " << list1 << endl;
    cout << "\tList2 is: " << list2 << endl;
    cout << "\tList3 is: " << list3 << endl;
}

void testString()
{
    AnyList<string> list1, list2, list3;

    list1.insert("What");
    list1.insert("a");
    list1.insert("beautiful");
    list1.insert("day");
    list1.insert("!");

    list2.insert("Isn't");
    list2.insert("it");
    list2.insert("?");

    list3.insert("Yes!");

    cout << "TEST: Ostream operator <<\n\n";
    cout << "\tList1 is: " << list1 << endl;
    cout << "\tList2 is: " << list2 << endl;
    cout << "\tList3 is: " << list3 << endl;
}


The AnyList.cpp and AnyList.h files originally didn't have any templates on them. Everything template related in those two files are what I added but I still have those two errors (again, not sure if template related).

Thanks!
Last edited on
With reference to your post above could you tell us on which lines of AnyList.cpp the 2 errrors mentioned appear?
When dealing with templates both the definition and the implementation must be in the same compilation unit, your implementations are not in the same compilation unit.


Good point!
Topic archived. No new replies allowed.