error C2995 while compiling

Hi,
1) When i`m trying to compile this, i`m getting this error:
node.h(48): error C2995: 'std::ostream &operator <<(std::ostream &,const Node<T> &)' : function template has already been defined
Any reason why ?

2) on Tree.h , there is a problem with _root = new Node<element>;
can you tell me why ?!

Movie.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
#include <iostream>
#include <string>
#include <ostream>
#include "Movie.h"	

using namespace std;

	int Movie::getScore() const {
		return _score;
	}

	string Movie::getName() const {
		return _name;
	}

	bool Movie::operator < (const Movie& movie2) const {
		if (this ==  &movie2) {
			return false;
		}
		if (getScore() < movie2.getScore() )
			return true;
		if (getScore() > movie2.getScore() )
			return false;

		if ((getName().compare(movie2.getName())) < 0 )
			return true;
		return false;
	}

	bool Movie::operator==(const Movie& movie2) const {
		if (*this ==  movie2) {
			return true;
		}
		if (!(getScore() == movie2.getScore() ) && !(getName() == movie2.getName()) ) // str1.compare(str2)
			return true;
		return false;
	}

	ostream& operator<<(ostream& out, const Movie& movie) {
		out << movie._name << "(" << movie._score << ")." << endl;
		return out;
	}

Movie.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
#ifndef _MOVIE_H_
#define _MOVIE_H_

#include <string>
#include <ostream>

using namespace std;

class Movie {
public:
	Movie(string name, int score);

	int getScore() const;
	string getName() const;
	
	/* movie1 is smaller than  movie2,
	*  if its score is smaller. 
	*  If the scores are the same, their names should be lexicographic compared*/
	bool operator<(const Movie& ) const;
	
	/* movie1 equals movie2 if their scores and names are the same */
	bool operator==(const Movie&) const;

	/* Should output the movie name and movie score */
	friend ostream& operator<<(ostream& out, const Movie& node);
private:
	int _score;
	string _name;
};

#endif 


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

#include <iostream>
#include <list>

using namespace std;

template <class T> 
class Node{
public:
	Node(){
		_parent = _left = _right = 0;
		_element = 0;
	}

	Node(T element){
		_parent = _left = _right = 0;
		_element = element;
	}

	Node& operator=(const Node& node){
		if (this == &node)
			return *this;
		delete[] _element;
		_element = new T;
		_element = node._element;
		return *this;
	}


	bool operator<(const Node& node) const {
		if (this == &node)
			return false;
		if (_element < node._element)
			return true;
		return false;
	}
	
	bool operator==(const Node& node) const {
		if (this == &node)
			return true;
		if (_element == node._element )
			return true;
		return false;
	}
	/*This function should call the operator<< of the node's element*/
	template <class T2> friend ostream& operator<<(ostream& out, const Node<T2>& node){
		out << node._element << endl;
		return out;
	}

//       parent set/get           //
	Node* getParent() const {
		return _parent;
	}

	void setParent(Node* node){
		_parent = node;
	}

//       right set/get           //
	Node* getRight() const{
		return _right;
	}

	void setRight(Node* node){
		_right - node;
	}

//       left set/get           //
	Node* getLeft() const{ 
		return _left;
	}

	void setLeft(Node* node) {
		_left = node;
	}

//       element set/get           //
	T getElement() const{
		return _element;
	}

	void setElement(T ele){
		_element = ele;
	}

private:
	Node* _parent;
	Node* _right;
	Node* _left;
	T _element;
};

#endif 


Tree.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef _TREE_H_
#define _TREE_H_

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

using namespace std;

template <class T>
class Tree{
public:
	Tree():_root(NULL){};

	/*Should return the root of the tree
	* if the tree is empty should return NULL
	*/
	Node<T>* getRoot() const{
		if (_root == NULL)
			return NULL;
		return _root;
	}

	/* Should return the node that contains the input paramter.
	* if such node does not exist, should return NULL
	*/	
	Node<T>* findNode(const T& element) const{
		Node<T>* tmp = _root;
		while (tmp != NULL) {
			if (tmp->getElement() == element) {
				return tmp;
			} else {
				if (tmp->getElement() < element) {
					tmp = tmp->getLeft();
				} else {
					tmp = tmp->getRight();
				}
			}
		}
		return NULL;
	}

	/*Should return the node with the maximal value
	* if the tree is empty should return NULL
	*/
	Node<T>* getMaximum() const{
		Node<T>* tmp = _root;
		while (tmp->getRight())
			tmp = tmp->getRight();
		return tmp;
	}

	/* Should delete the node with input parameter from the tree, 
	 * if a node with this value does not exist, returns false 
	 * if the deletion succeeded returns true
	 * otherwise, returns false 
	 */
	bool deleteElement(const T& element){
		Node<T>* tmp;//= findNode(T&);
		tmp = findNode(element);
		return deleteAndFix(tmp);
	}

	/* Should delete the node todelete from the tree, 
	 * if the deletion succeeded returns true
	 * otherwise, returns false 
	 */
	bool deleteNode(Node<T>* todelete){
		Node<T>* tmp = findNode(todelete->_element);
		return deleteAndFix(tmp);
	}

	/*Should add a node with input value to the tree
	* if a node with the same value exists, the function should return false
	* otehrwise, it should return true
	*/
	bool addElement(const T& element){
		if (_root == NULL){
			_root = new Node<element> ; //Here 
			_root->setElement(element);
			return true;
		}
		if (findNode(element))
			return false;
		Node<T>* tmp = _root, next = NULL;
		bool isLeft = true;
		do {
			if (tmp->getElelemt() < element){
				next = tmp->getLeft();
				isLeft = true;
			}
			else {
				next = tmp->getRight();
				isLeft = false;
			}
		} while (next != NULL);
		next = new Node<element>;
		next->setElement(element);
		next->setParent(tmp);
		if (isLeft)
			tmp->setLeft(next);
		else
			tmp->setRight(next);
	}


	/*Should print the tree in ascending order. 
	* (first value is the smallest node in the tree)
	* if the tree is empty, it should print 'The tree is empty'
	*/
	void printTree() const{
		if (_root == NULL){
			cout << "The Tree is Empty." << endl;
			return;
		}
	}


private:
	Node<T>* _root;
//	void inorder(Node<T>*) const;
	bool deleteAndFix(Node<T>* tmp){
		if (tmp == NULL)
			return false;
		if (tmp->getRight() == NULL){
			if (tmp->getLeft() == NULL){
				delete tmp;
				return true;
			} else {
				tmp->getLeft()->setParent(tmp->getParent());
				tmp->getParent()->setLeft(tmp->getLeft());
				delete tmp;
				return true;
			}
		} else {
			Node<T>* tmp2 = tmp;
			tmp = tmp->getRight();
			while (tmp->getLeft() != NULL)
				tmp = tmp ->getLeft();
			if (tmp->getParent() != tmp2)
				tmp->getParent->setLeft(NULL);
			tmp->setRight(tmp2->getRight());
			tmp->setLeft(tmp2->getLeft());
			tmp->setParent(tmp2->getParent());
			if (tmp2->getLeft() != NULL) 
				tmp2->getLeft()->setParent(tmp);
			tmp2->getRight()->setParent(tmp);
			if (tmp2->getParent()->getLeft() == tmp2)
				tmp2->getParent()->setLeft(tmp);
			else
				tmp2->getParent()->setRight(tmp);
			delete tmp2;
			return true;
		}
		return false;
	}
};

#endif	 
Last edited on
1. First is because when you have different types for T, operator<T2> << is defined for each of them. Since T2 is not the same as T, cout << Node<float> could call a << defined in Node<float> as well as the one in Node<int>, or etc. I think.. To solve this, try removing T2 and replacing it with T.

2. in _root = new Node<element>, element is not a type. This should probably be new Node<T>(element) instead.
Topic archived. No new replies allowed.