Estoy programando un árbol binario, pero al momento de compilar el programa, la función virtual no la reconoce o no se linkea, ¿a qué se debe? o que hago mal? ojalá puedan ayudarme, gracias!
BTNode.h :
Código
#define BTNODE_H_INCLUDED #include <sstream> using namespace std; template<typename IT> struct BTNode { IT data; BTNode<IT>* left; BTNode<IT>* right; BTNode(const IT &Data, BTNode<IT>* lv = NULL, BTNode<IT>* rv = NULL) : data(Data), left(lv), right(rv) {} virtual ~BTNode() {} virtual string toString() const { ostringstream os; os << data; return os.str(); } }; template <typename IT> ostream &operator<<(ostream &out, const BTNode<IT> &node) { return out << node.toString(); } #endif // BTNODE_H_INCLUDED
Binary_Tree.h:
Código
#ifndef BINARY_TREE_H_INCLUDED #define BINARY_TREE_H_INCLUDED #include <cstddef> #include <sstream> #include <stdexcept> #include <string> #include "BTNode.h" using namespace std; template <typename IT> class Binary_Tree { public: Binary_Tree(): root(NULL) {} Binary_Tree(const IT &Data, const Binary_Tree<IT> &lc = Binary_Tree(), const Binary_Tree<IT> &rc = Binary_Tree()): root(new BTNode<IT>(Data, lc.root, rc.root)) {} virtual ~Binary_Tree() {} Binary_Tree<IT> getLeftSubtree() const; Binary_Tree<IT> getRigthSubtree() const; const IT &getData() const; bool isNull() const; bool isLeaf() const; virtual string toString() const; //<---esta es la función!!!! static Binary_Tree<IT> readBinaryTree(istream& in); Binary_Tree(BTNode<IT>* newRoot): root(newRoot) {} protected: BTNode<IT>* root; }; template<typename IT> ostream &operator<<(ostream &out, const Binary_Tree<IT> &tree) { return out << tree.toString(); } template<typename IT> istream &operator>>(istream &in, Binary_Tree<IT> &tree) { tree = Binary_Tree<IT>::readBinaryTree(in); return in; } #endif // BINARY_TREE_H_INCLUDED
Binary_Tree.cpp
Código
#include "Binary_Tree.h" using namespace std; template<typename IT> Binary_Tree<IT> Binary_Tree<IT>::getLeftSubtree() const { if(root == NULL) { throw invalid_argument("getLeftSubtree on empty tree"); } return Binary_Tree<IT>(root->left); } template<typename IT> Binary_Tree<IT> Binary_Tree<IT>::getRigthSubtree() const { if(root == NULL) { throw invalid_argument("getLeftSubtree on empty tree"); } return Binary_Tree<IT>(root->right); } template<typename IT> bool Binary_Tree<IT>::isLeaf() const { if(root != NULL) { return root->left == NULL && root->right == NULL; } else { return true; } } template<typename IT> string Binary_Tree<IT>::toString() const { ostringstream os; if(isNull()) { os << "NULL\n"; } else { os << *root << "\n"; os << getLeftSubtree().toString(); os << getRigthSubtree().toString(); } return os.str(); }