I'm completely new to cpp, and am trying to write a simple Binary Tree. I've done minimal implementation on insert function as below.
#include <iostream>
using namespace std;
template <typename T, typename U>
class MyNode {
public :
T key;
U value;
MyNode<T,U> * left;
MyNode<T,U> * right;
MyNode<T,U>(const T& k, const U& v)
{
key = k;
value = v;
left = nullptr;
right = nullptr;
}
};
template<typename T, typename U>
class MyTree {
public :
MyNode<T,U>* root = nullptr;
bool insert(const T& key, const U& value);
void check(MyNode<T,U>*& node) {
if (!node) return;
std::cout<<node->key<<": "<<node->value<<std::end;
}
private :
bool insert(MyNode<T,U>*& node, const T& key, const U& value);
};
template<typename T, typename U>
bool MyTree<T,U>::insert(const T& key, const U& value) {
return insert(root, key, value);
}
template<typename T, typename U>
bool MyTree<T,U>::insert(MyNode<T,U>*& node, const T& key, const U& value) {
if (node == NULL) {
MyNode<T,U>* newnode;
newnode->key = key;
newnode->value = value;
return true;
}
else {
if (value < node->value) {
insert(node->left, key, value);
}
else {
insert(node->right, key, value);
}
}
}
The main.cpp file below uses the ctest.h
file above.
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <iostream>
#include "ctest.h"
using namespace std;
int main () {
MyTree<string, int> tree;
tree.insert("phil", 12);
tree.check(tree.root);
return 0;
}
However, when I try to compile the code with command g++ -std=c++11 main.cpp
, I'm getting the error below
In file included from main.cpp:6:0:
ctest.h: In instantiation of 'void MyTree<T, U>::check(MyNode<T, U>*&) [with T = std::basic_string<char>; U = int]':
main.cpp:13:22: required from here
ctest.h:29:43: error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and '<unresolved overloaded function type>')
std::cout<<node->key<<": "<<node->value<<std::end;
^
ctest.h:29:43: note: candidates are:
The node value's type will be int, as I've defined the tree as MyTree<string, int>
, and int type should have the operator <<. Any points I'm missing ? Thank you!
Aucun commentaire:
Enregistrer un commentaire