首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

error:is not a member of 'int'

2013-03-20 
求助:error:is not a member of 'int'本帖最后由 accony 于 2013-03-14 11:06:34 编辑小弟在学

求助:error:is not a member of 'int'
本帖最后由 accony 于 2013-03-14 11:06:34 编辑 小弟在学习二叉搜索树。遇到一个问题,自己弄了好久没明白该怎么改。恳请大家帮忙看看该改哪句话
编译后出错:exercise5_11.h:126: error: ‘lt’ is not a member of ‘int’
以下是exercise5_11.h文件
#ifndef EXERCISE5_11_H
#define EXERCISE5_11_H
 
#include <iostream>
#include <string.h>

using namespace std;

/*template <class Key, class Elem, class KEComp>
class Dictionary {
 public:
  virtual void clear() = 0;
  virtual bool insert(const Elem&) = 0;
  virtual bool remove(const Key&, Elem&) = 0;
  virtual bool removeAny(Elem&) = 0;
  virtual bool find(const Key&, Elem&) const = 0;
  virtual int size() = 0;
  };*/

template <class Key, class Elem>
class KEComp {
 public:
  static bool lt(Key x, Elem y) { return x < y; }
  static bool eq(Key x, Elem y) { return x == y; }
  static bool gt(Key x, Elem y) { return x > y; }
};

template <class Elem>
class EEComp {
 public:
  static bool lt(Elem x, Elem y) { return x < y; }
  static bool eq(Elem x, Elem y) { return x == y; }
  static bool gt(Elem x, Elem y) { return x > y; }
};

template <class Elem> 
  class BinNode {
 public:
  virtual Elem& val() = 0;
  virtual void setVal(const Elem&) = 0;
  virtual BinNode* left() const = 0;
  virtual void setLeft(BinNode*) = 0;
  virtual BinNode* right() const = 0;
  virtual void setRight(BinNode*) = 0;
  virtual bool isLeaf() = 0;
  int height;
};

template <class Elem>
class BinNodePtr : public BinNode<Elem> {
 private:
  Elem it;
  BinNodePtr* lc;
  BinNodePtr* rc;
 public:
  BinNodePtr() {lc = rc = NULL;}
  BinNodePtr(Elem e, BinNodePtr* l = NULL, BinNodePtr* r = NULL)
    { it = e; lc = l; rc = r; }
  ~BinNodePtr() {}
  Elem& val() {return it;}
  void setVal(const Elem& e) { it = e; }
  inline BinNode<Elem>* left() const {return lc;}
  void setLeft(BinNode<Elem>* b) {lc = (BinNodePtr*)b;}
  inline BinNode<Elem>* right() const {return rc;}
  void setRight(BinNode<Elem>* b) {rc = (BinNodePtr*)b;}
  bool isLeaf() {return (lc == NULL) && (rc == NULL);}


};

template <class Key, class Elem, class KEComp, class EEComp>
class BST {
 private:
  BinNode<Elem>* root;
  int nodecount;
  void clearhelp(BinNode<Elem>*);
  BinNode<Elem>* inserthelp(BinNode<Elem>*,const Elem&);
  BinNode<Elem>* deletemin(BinNode<Elem>*,BinNode<Elem>*&);
  BinNode<Elem>* removehelp(BinNode<Elem>*,const Key&,BinNode<Elem>*&);
  bool findhelp(BinNode<Elem>*,const Key&,Elem&) const;
  void printhelp(BinNode<Elem>*,int) const;
 public:
  BST() {root = NULL; nodecount = 0; }
  ~BST() {clearhelp(root);}
  void clear()
  {
    clearhelp(root);root = NULL;nodecount = 0;
  }
  bool insert(const Elem& e) {
    root = inserthelp(root,e);
    nodecount++;
    return true;
  }
  bool remove(const Key& K, Elem& e) {
    BinNode<Elem>* t = NULL;
    root = removehelp(root, K, t);
    if (t == NULL) return false;
    e = t->val();
    nodecount--;
    delete t;
    return true;
  }
  bool removeAny(Elem& e) {
    if (root == NULL) return false;
    BinNode<Elem>* t;
    root = deletemin(root,t);
    e = t->val();
    nodecount--;
    delete t;
    return true;
  }
  bool find(const Key& K, Elem& e) const {
    return findhelp(root, K, e);
  }
  int size() {return nodecount;}
  void print() const {
    if (root == NULL) cout << "\n The BST is empty.\n";
    else printhelp(root, 0);
  }
  
};

template <class Key, class Elem, class KEComp, class EEComp>
  BinNode<Elem>* BST<Key,Elem,KEComp,EEComp>::
inserthelp(BinNode<Elem>* subroot, const Elem& val) {
  if (subroot == NULL)
    return (new BinNodePtr<Elem>(val, NULL, NULL));
  if (EEComp::lt(val, subroot->val()))
    subroot->setLeft(inserthelp(subroot->left(),val));
  else subroot->setRight(inserthelp(subroot->right(),val));
  return subroot;
}

template <class Key, class Elem, class KEComp, class EEComp>
  BinNode<Elem>* BST<Key,Elem,KEComp,EEComp>::
deletemin(BinNode<Elem>* subroot, BinNode<Elem>*& min) {
  if (subroot->left() == NULL) {


    min = subroot;
    return subroot->right();
  }
  else {
    subroot->setLeft(deletemin(subroot->left(), min));
    return subroot;
  }
}

template <class Key, class Elem, class KEComp, class EEComp>
  BinNode<Elem>* BST<Key,Elem,KEComp,EEComp>::
removehelp(BinNode<Elem>* subroot, const Key& K, BinNode<Elem>*& t) {
  if (subroot == NULL) return NULL;
  else if (KEComp::lt(K, subroot->val()))
    subroot->setLeft(removehelp(subroot->left(), K, t));
  else if (KEComp::gt(K, subroot->val()))
    subroot->setRight(removehelp(subroot->right(), K, t));
  else {
    BinNode<Elem>* temp;
    t = subroot;
    if (subroot->left() == NULL)
      subroot = subroot->right();
    else if (subroot->right() == NULL)
      subroot = subroot->left();
    else {
      subroot->setRight(deletemin(subroot->right(),temp));
      Elem te = subroot->val();
      subroot->setVal(temp->val());
      temp->setVal(te);
      t = temp;
    }
  }
  return subroot;
}

template <class Key, class Elem, class KEComp, class EEComp>
  void BST<Key,Elem,KEComp,EEComp>::
clearhelp(BinNode<Elem>* subroot) {
  if (subroot == NULL) return;
  clearhelp(subroot->left());
  clearhelp(subroot->right());
  delete subroot;
}

template <class Key, class Elem, class KEComp, class EEComp>
  void BST<Key,Elem,KEComp,EEComp>::
printhelp(BinNode<Elem>* subroot, int level) const {
  if (subroot == NULL) return;
  printhelp(subroot->left(), level + 1);
  for (int i=0; i<level; i++)
    cout << " ";
  cout << subroot->val() << "\n";
  printhelp(subroot->right(), level + 1);
}

#endif /*EXERCISE5_11_H_*/

//--------------以下是exercise5_11.cpp文件----------------------
#include <iostream>
#include <string.h>
#include "exercise5_11.h"

//template <class Key, class Elem, class KEComp, class EEComp> class Dictionary;
template <class Elem> class Key;
template <class Key, class Elem> class KEComp;
template <class Elem> class EEComp;
template <class Elem> class BinNode;
template <class Elem> class BinNodePtr;
template <class Key, class Elem, class KEComp, class EEComp> class BST;


using namespace std;

int main()
{
  //Key <int> *intKey = new Key <int>();
  KEComp <int, int> *intKEComp = new KEComp <int, int>();
  EEComp <int> *intEEComp = new EEComp <int>();
  /*BinNode <int> *intBN1 = new BinNode <int>();
  BinNode <int> *intBN1 = new BinNode <int>();
  BinNode <int> *intBN1 = new BinNode <int>();
  BinNode <int> *intBN1 = new BinNode <int>();
  BinNode <int> *intBN1 = new BinNode <int>();*/
  BST <int,int,int,int> *intBST = new BST <int,int,int,int>();//感觉是这一句我写得不对。但是我实在不知道该怎么写了
  
  intBST->insert(37);
  intBST->insert(24);
  intBST->insert(42);
  intBST->insert(7);
  intBST->insert(32);
  intBST->insert(2);
  intBST->insert(40);
  intBST->insert(42);
  intBST->insert(120);

  intBST->print();

  
}
[解决办法]
声明改成这样。直接用对象把,用指针还得 delete,多麻烦。


BST <int,int,KEComp<int,int>,EEComp<int>> intBST;

热点排行
Bad Request.