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 ?!
#ifndef _MOVIE_H_
#define _MOVIE_H_
#include <string>
#include <ostream>
usingnamespace 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*/
booloperator<(const Movie& ) const;
/* movie1 equals movie2 if their scores and names are the same */
booloperator==(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
#ifndef _TREE_H_
#define _TREE_H_
#include <iostream>
#include "Node.h"
usingnamespace 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);
returntrue;
}
if (findNode(element))
returnfalse;
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)
returnfalse;
if (tmp->getRight() == NULL){
if (tmp->getLeft() == NULL){
delete tmp;
returntrue;
} else {
tmp->getLeft()->setParent(tmp->getParent());
tmp->getParent()->setLeft(tmp->getLeft());
delete tmp;
returntrue;
}
} 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;
returntrue;
}
returnfalse;
}
};
#endif
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.