templated class problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jico12
    New Member
    • Mar 2010
    • 1

    templated class problem

    hello,

    I am trying to write a program that uses two templated classes, one call Rek, which represents a record; and another called Table, which consists of an array of pointers to instances of Rek -- basically, a table for storing data.
    I'm getting an error at compile time that seems to indicate that it's trying to convert a pointer to a variable, but i can't understand why.
    the error is:
    rek.t: In constructor `Rek<K, D>::Rek() [with K = Word, D = Word]':
    table.t:41: instantiated from `Table<K, D>::Table() [with K = Word, D = Word]'
    p3.cc:25: instantiated from here
    rek.t:32: error: invalid conversion from `Word*' to `char'
    rek.t:32: error: initializing argument 1 of `Word::Word(cha r)'
    rek.t:33: error: invalid conversion from `Word*' to `char'
    rek.t:33: error: initializing argument 1 of `Word::Word(cha r)'

    The Table class appears as follows, with the erroring line commented:
    Code:
    #ifndef TABLE_T
    #define TABLE_T
    #include"rek.t"
    #include"check.h"
    
    template<class K, class D>
    class Table{
    private:
      Rek<K,D> **root;
      int size;
      int loc;
    public:
      Table();
      Table(const Table &t);
      Table(int sz);
      ~Table();
      //void kill(Rek<K,D> *&rt)
      const Table & operator =(const Table &t);
      void copy(Rek<K,D> **from, Rek<K,D> **&to);
      Table & add(const K &k, const D & d = D());
      void add(const K &k, const D &d, Rek<K,D> **&rt);
      D & operator [](const K &k);
      const D & operator [](const K &k)const;
      Rek<K,D> * findRek(const K &k);
      bool inTable(const K &k)const;
      template<class X, class Y>
      friend ostream & operator <<(ostream &out, const Table<X,Y> &t);
      ostream & display(ostream & out, Rek<K,D> **rt)const;
    };
    
    template<class K, class D>
    Table<K,D>::Table(){
      loc = 0;
      size = 0;
      root[loc] = new Rek<K,D>();   //line 41
      
    }
    
    template<class K, class D>
    Table<K,D>::Table(const Table &t){
      loc += 1;
      root[loc] = new Rek<K,D>();
      copy(t.root,root);
    }
    
      template<class K, class D>
      Table<K,D>::Table(int sz){
        // int i = 0;
        // for(int j = 0; j < sz; j++)
        // root[j] = new Rek<K,D>();
        loc = 0;
        root[loc] = NULL;
        size = sz;
      }
    
    template<class K, class D>
    Table<K,D>::~Table(){
      for(int i = 0; i < size; i ++)
        delete root[i];
    }
    
    /*template<class K, class D>
    void Table<K,D>::kill(Rek<K,D> *&rt){
      if(rt != NULL)
        delete rt;
        }*/
    
    template<class K, class D>
    const Table<K,D> & Table<K,D>::operator =(const Table & t){
      root = NULL;
      copy(t.root, root);
      return *this;
    }
    
    template<class K, class D>
    void Table<K,D>::copy(Rek<K,D> **from, Rek<K,D> **&to){
      if(from != NULL){
        to[loc] = new Rek<K,D>(from[loc]->getKey(), from[loc]->data);
        //for(int j = 0; j < size; j++)
          //to[j] = new Rek<K,D>(from[j]->getKey(), from[j]->data);
        check(to != NULL, "Heap overflow", __LINE__, __FILE__);
    }
    }
    
    template<class K, class D>
    Table<K,D> &Table<K,D>::add(const K &k, const D &d){
      add(k,d,root);
      return *this;
    }
    
    template<class K, class D>
    void Table<K,D>::add(const K &k, const D &d, Rek<K,D> **&rt){
      if(rt == NULL){
        //*rt = new Rek<K,D> (k,d);
        rt[loc] = new Rek<K,D>(k,d);
        check(rt != NULL, "Heap overflow", __LINE__, __FILE__);
      }
      else
        check(false, "duplicate key", __LINE__, __FILE__);
    }
    
    template<class K, class D>
    D & Table<K,D>::operator[](const K &k){
      if(!inTable(k))
        add(k);
      return findRek(k)->data;
    }
    
    template<class K, class D>
    const D & Table<K,D>::operator[](const K &k)const{
      check(inTable(k), "Missing key", __LINE__, __FILE__);
      return findRek(k)->data;
    }
    
    template<class K, class D>
    Rek<K,D> *Table<K,D>::findRek(const K &k){
      Rek<K,D> **temp;
      temp = root;
      int i = 0;
      int j = 0;
      while(temp[i][j].getKey() != k && i < loc){
        temp[i][j] = root[i][j];
        i++;
      }
      return temp[i];
    }
      template<class K, class D>
    bool Table<K,D>::inTable(const K &k)const{
      Rek<K,D> **temp;
      temp = root;
      int i = 0;
      int j = 0;
      while(temp[i][j].getKey() != k && i < loc){
        temp[i][j] = root[i][j];
        i++;
      }
      return temp != NULL;
    }
    
    template<class X, class Y>
    ostream &operator<<(ostream &out, const Table<X,Y> &t){
      return t.display(out, t.root);
    }
    
      template<class K, class D>
    ostream & Table<K,D>::display(ostream &out, Rek<K,D> **rt)const{
      if(rt != NULL){
        for(int j = 0; j < loc; j++)
          out<<rt[j]<<endl;
      }
      return out;
      }
    
    #endif
    and the Rek class as follows:

    Code:
    #ifndef REK_T
    #define REK_T
    #include<iostream>
    using namespace std;
    
    template <class K,class D>
    class Rek{
    private:
      K key;
    public:
      D data;
      K getKey()const;
      Rek();
      Rek(const K &k, const D &d = D());
      template<class X, class Y>
      friend ostream &operator<<(ostream &out, const Rek<X,Y> &r);
    };
    
    template<class X, class Y>
    X Rek<X,Y>::getKey()const{
      return key;
    }
    
    template<class K, class D>
    Rek<K,D>::Rek(){
      key = new K();  //line 32
      data = new D(); //line 33
    }
    template<class K, class D>
    Rek<K,D>::Rek(const K &k, const D &d):key(k),data(d){
    }
    
    template<class X, class Y>
    ostream &operator<<(ostream &out, const Rek<X,Y> &r){
      return out<<"{"<<r.key<<", "<<r.data<<"}"<<endl;
    }
    
    #endif
    Thank you
    Last edited by Banfa; Mar 4 '10, 09:23 AM. Reason: Added code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In your class Rek data and key are not defined as pointers, yet you are trying to assign new'd to them.

    I suggest you have missing *s at lines 9 and 11 (from the numbering in the code listings above).

    Comment

    Working...