elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a Git (Primera Parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  [Consulta] Arboles binarios
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Consulta] Arboles binarios  (Leído 1,375 veces)
Gunhack

Desconectado Desconectado

Mensajes: 42



Ver Perfil
[Consulta] Arboles binarios
« en: 11 Noviembre 2015, 04:44 am »

Buenas a quien lo lea!

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
  1. #define BTNODE_H_INCLUDED
  2.  
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. template<typename IT>
  8. struct BTNode {
  9.  
  10.  IT data;
  11.  BTNode<IT>* left;
  12.  BTNode<IT>* right;
  13.  
  14.  BTNode(const IT &Data, BTNode<IT>* lv = NULL, BTNode<IT>* rv = NULL) :
  15.    data(Data), left(lv), right(rv) {}
  16.  
  17.  virtual ~BTNode() {}
  18.  
  19.  virtual string toString() const {
  20.  
  21.    ostringstream os;
  22.  
  23.    os << data;
  24.    return os.str();
  25.  }
  26. };
  27.  
  28. template <typename IT>
  29. ostream &operator<<(ostream &out, const BTNode<IT> &node) {
  30.  
  31.  return out << node.toString();
  32. }
  33.  
  34. #endif // BTNODE_H_INCLUDED
  35.  
  36.  


Binary_Tree.h:
Código
  1.  
  2. #ifndef BINARY_TREE_H_INCLUDED
  3. #define BINARY_TREE_H_INCLUDED
  4.  
  5. #include <cstddef>
  6. #include <sstream>
  7. #include <stdexcept>
  8. #include <string>
  9. #include "BTNode.h"
  10.  
  11. using namespace std;
  12.  
  13. template <typename IT>
  14. class Binary_Tree {
  15.  
  16. public:
  17.  
  18.  Binary_Tree(): root(NULL) {}
  19.  Binary_Tree(const IT &Data, const Binary_Tree<IT> &lc = Binary_Tree(), const Binary_Tree<IT> &rc = Binary_Tree()):
  20.    root(new BTNode<IT>(Data, lc.root, rc.root)) {}
  21.  
  22.  virtual ~Binary_Tree() {}
  23.  
  24.  Binary_Tree<IT> getLeftSubtree() const;
  25.  Binary_Tree<IT> getRigthSubtree() const;
  26.  const IT &getData() const;
  27.  
  28.  bool isNull() const;
  29.  bool isLeaf() const;
  30.  
  31.  virtual string toString() const; //<---esta es la función!!!!
  32.  
  33.  static Binary_Tree<IT> readBinaryTree(istream& in);
  34. Binary_Tree(BTNode<IT>* newRoot): root(newRoot) {}
  35. protected:
  36.  
  37.  
  38.  BTNode<IT>* root;
  39. };
  40.  
  41. template<typename IT>
  42. ostream &operator<<(ostream &out, const Binary_Tree<IT> &tree) {
  43.  
  44.  return out << tree.toString();
  45. }
  46.  
  47. template<typename IT>
  48. istream &operator>>(istream &in, Binary_Tree<IT> &tree) {
  49.  
  50.  tree = Binary_Tree<IT>::readBinaryTree(in);
  51.  return in;
  52. }
  53.  
  54. #endif // BINARY_TREE_H_INCLUDED
  55.  
  56.  

Binary_Tree.cpp
Código
  1. #include "Binary_Tree.h"
  2.  
  3. using namespace std;
  4.  
  5. template<typename IT>
  6. Binary_Tree<IT> Binary_Tree<IT>::getLeftSubtree() const {
  7.  
  8.  if(root == NULL) {
  9.  
  10.    throw invalid_argument("getLeftSubtree on empty tree");
  11.  }
  12.  
  13.  return Binary_Tree<IT>(root->left);
  14. }
  15.  
  16. template<typename IT>
  17. Binary_Tree<IT> Binary_Tree<IT>::getRigthSubtree() const {
  18.  
  19.  if(root == NULL) {
  20.  
  21.    throw invalid_argument("getLeftSubtree on empty tree");
  22.  }
  23.  
  24.  return Binary_Tree<IT>(root->right);
  25. }
  26.  
  27. template<typename IT>
  28. bool Binary_Tree<IT>::isLeaf() const {
  29.  
  30.  if(root != NULL) {
  31.  
  32.    return root->left == NULL && root->right == NULL;
  33.  } else {
  34.  
  35.    return true;
  36.  }
  37. }
  38.  
  39. template<typename IT>
  40. string Binary_Tree<IT>::toString() const {
  41.  
  42.  ostringstream os;
  43.  
  44.  if(isNull()) {
  45.  
  46.    os << "NULL\n";
  47.  } else {
  48.  
  49.    os << *root << "\n";
  50.    os << getLeftSubtree().toString();
  51.    os << getRigthSubtree().toString();
  52.  }
  53.  
  54.  return os.str();
  55. }
  56.  
  57.  


En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: [Consulta] Arboles binarios
« Respuesta #1 en: 11 Noviembre 2015, 08:47 am »

En primer lugar, coloca el error.

Luego, ¿Por qué métodos virtuales? ¿Funciona correctamente sin ser virtual?


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
¿Arboles binarios en VB?....¿imposible?
Programación Visual Basic
GRANLUPIN23 0 3,018 Último mensaje 4 Diciembre 2007, 20:07 pm
por GRANLUPIN23
Arboles binarios
Programación C/C++
josue_tux 3 9,964 Último mensaje 9 Mayo 2010, 20:18 pm
por leogtz
Arboles binarios
Java
soser 0 1,881 Último mensaje 27 Octubre 2010, 08:45 am
por soser
ayuda en arboles binarios
Programación C/C++
josue_tux 1 2,798 Último mensaje 25 Junio 2011, 14:10 pm
por satu
Arboles Binarios c++
Programación C/C++
maferavagar 3 2,470 Último mensaje 8 Julio 2015, 11:20 am
por ivancea96
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines