Overloading method for a specific type in a template class

Hi fellow programmers of cplusplus! I am wondering if it is possible to have an overloaded function of a specific type in a template class. If it is possible, how to declare such function? For example:

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
#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H

#include <iostream>
#include "Map.h"
#include "BinaryTree.h"

using namespace std;

template<class T> class BinarySearchTree: public BinaryTree<T>
{
public:
	BinarySearchTree();
	~BinarySearchTree();

	void insert(Node<T> *newNode, Node<T> *parentNode);
	void insert(Node<Map> *newNode, Node<Map> *parentNode); // ???????
};

template<class T> BinarySearchTree<T>::BinarySearchTree(): BinaryTree() { }

template<class T> BinarySearchTree<T>::~BinarySearchTree() { }

template<class T> void BinarySearchTree<T>::insert(Node<T> *newNode, Node<T> *parentNode)
{
	if(newNode->info < parentNode->info)
	{
		if(parentNode->lLink == NULL)
		{
			parentNode->lLink = newNode;
		}
		else
		{
			insert(newNode, parentNode->lLink);
		}
	}
	else
	{
		if(parentNode->rLink == NULL)
		{
			parentNode->rLink = newNode;
		}
		else
		{
			insert(newNode, parentNode->rLink);
		}
	}
}

template<class T> void BinarySearchTree<Map>::insert(Node<Map> *newNode, Node<Map> *parentNode)
{
	cout << "";
}
#endif 
IIRC
1
2
3
4
template <> //specialization
void BinarySearchTree<Map>::insert(Node<Map> *newNode, Node<Map> *parentNode){
//...
}
Doing a

1
2
3
4
template <> //specialization
void BinarySearchTree<Map>::insert(Node<Map> *newNode, Node<Map> *parentNode){
//...
}


will have a compilation error stating that the overloaded function clashes with
void insert(Node<T> *newNode, Node<T> *parentNode);
I'm not having any issues here.
Please copy-paste the compiler message, along with your updated code.
These are the updated files:

BinaryTree.h (Base class)
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
#ifndef BINARYTREE_H
#define BINARYTREE_H

#include <iostream>
#include <algorithm>
#include "Map.h"

using namespace std;

template<class T> struct Node
{
	T info;
	Node<T> *lLink;
	Node<T> *rLink;
};

template<class T> class BinaryTree
{
public:
	BinaryTree();
	~BinaryTree();

	bool isEmpty();
	
	int treeHeight() const;
	int treeNodeCount() const;

	void treeDestroy() const;
	void inorderTraversal() const;
	void preorderTraversal() const;
	void postorderTraversal() const;
	
        // Abstract functions
	virtual void treeInsert(const T &insertItem) = 0; 
	virtual void treeInsert(const T &insertItem1, const T &insertItem2) = 0;
        // end of abstract functions

protected:
	Node<T> *root;
	
	// Abstract functions
	virtual void insert(Node<T> *newNode, Node<T> *parentNode) = 0;
	virtual void insert(Node<Map> *newNode, Node<Map> *parentNode) = 0;
	// end of abstract functions

private:
	
	int height(Node<T> *p) const;
	int nodeCount(Node<T> *p) const;

	void nodeDestroy(Node<T> *p) const;
	void inorder(Node<T> *p) const;
	void preorder(Node<T> *p) const;
	void postorder(Node<T> *p) const;

};

// declaration not included
#endif 


BinarySearchTree.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
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
92
93
94
95
96
97
98
99
100
#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H

#include <iostream>
#include "BinaryTree.h"

using namespace std;

template<class T> class BinarySearchTree: public BinaryTree<T>
{
public:
	BinarySearchTree();
	~BinarySearchTree();

        // used function from problem area
	void treeInsert(const T &insertItem);
	void treeInsert(const T &insertItem1, const T &insertItem2);
        // end used function area
private:
        // problem area
	void insert(Node<T> *newNode, Node<T> *parentNode);
	void insert(Node<Map> *newNode, Node<Map> *parentNode);
        // problem area
};
#endif

template<class T> BinarySearchTree<T>::BinarySearchTree(): BinaryTree() { }

template<class T> BinarySearchTree<T>::~BinarySearchTree() { }

template<class T> void BinarySearchTree<T>::treeInsert(const T &insertItem)
{
	Node<T> *newNode = new Node<T>;
    
	// initialize new node
	newNode->info = insertItem;
	newNode->lLink = NULL;
	newNode->rLink = NULL;
    
	// if tree is empty, make new node the root node 
	if(isEmpty())
	{
		root = newNode;
	}
	else
	{
		insert(newNode, root);
	}
}

template<class T> void BinarySearchTree<T>::insert(Node<T> *newNode, Node<T> *parentNode)
{
	if(newNode->info <= parentNode->info)
	{
		if(parentNode->lLink == NULL) // insert left
		{
			parentNode->lLink = newNode;
		}
		else
		{
			insert(newNode, parentNode->lLink);
		}
	}
	else
	{
		if(parentNode->rLink == NULL) // insert right
		{
			parentNode->rLink = newNode;
		}
		else
		{
			insert(newNode, parentNode->rLink);
		}
	}
}

template<T> void treeInsert(const T &insertItem1, const T &insertItem2)
{
	Node<Map> *newNode = new Node<Map>;

	newNode->info.insert("Date", insertItem1);
	newNode->info.insert("Reading", insertItem2);
	newNode->lLink = NULL;
	newNode->rLink = NULL;
    
	// if tree is empty, make new node the root node 
	if(isEmpty())
	{
		root = newNode;
	}
	else
	{
		insert(newNode, root);
	}
}

template<> void BinarySearchTree<Map>::insert(Node<Map> *newNode, Node<Map> *parentNode)
{
	cout << ""; //todo
}


The compiler churned out this:
ClCompile:
Main.cpp
c:\users\faizal rahman\documents\visual studio 2010\projects\assg2v2\assg2v2\binarytree.h(75): error C2535: 'void BinaryTree<T>::insert(Node<T> *,Node<T> *)' : member function already defined or declared
with
[
T=Map
]
c:\users\faizal rahman\documents\visual studio 2010\projects\assg2v2\assg2v2\binarytree.h(74) : see declaration of 'BinaryTree<T>::insert'
with
[
T=Map
]

Build FAILED.
You can only overload a whole class for a specific type. You can't overload a single method.
TheDestroyer: If i overload a whole class, i can't overload a constructor because it doesn't have any arguments. I've solved the problem by creating another class of <Map> type that has its own declaration of insert() etc. It's looks ugly.
Don't declare the specialization. Just put it in a correspondent cpp

You can specialize the whole class too.
Last edited on
>> will have a compilation error stating that the overloaded function clashes with

> You can only overload a whole class for a specific type. You can't overload a single method.

1
2
template <>
void insert(Node<Map> *newNode, Node<Map> *parentNode);

is an explicit instantiation of a member function of the class. And the C++ standard does not allow explicit instantiation of a member of a class at class scope.

For a work-around, see: http://cplusplus.com/forum/general/58906/
Thanks gang. Appreciate all the help given. I will try all the advices given tomorrow. :)
ne555: You're right. I managed to overload a single method using

1
2
template <>
void insert(Node<Map> *newNode, Node<Map> *parentNode);


I made an error in the base class by not doing specialization on the basic functions.

Thank again all.
Topic archived. No new replies allowed.