473,486 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

templated class problem

1 New Member
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(char)'
rek.t:33: error: invalid conversion from `Word*' to `char'
rek.t:33: error: initializing argument 1 of `Word::Word(char)'

The Table class appears as follows, with the erroring line commented:
Expand|Select|Wrap|Line Numbers
  1. #ifndef TABLE_T
  2. #define TABLE_T
  3. #include"rek.t"
  4. #include"check.h"
  5.  
  6. template<class K, class D>
  7. class Table{
  8. private:
  9.   Rek<K,D> **root;
  10.   int size;
  11.   int loc;
  12. public:
  13.   Table();
  14.   Table(const Table &t);
  15.   Table(int sz);
  16.   ~Table();
  17.   //void kill(Rek<K,D> *&rt)
  18.   const Table & operator =(const Table &t);
  19.   void copy(Rek<K,D> **from, Rek<K,D> **&to);
  20.   Table & add(const K &k, const D & d = D());
  21.   void add(const K &k, const D &d, Rek<K,D> **&rt);
  22.   D & operator [](const K &k);
  23.   const D & operator [](const K &k)const;
  24.   Rek<K,D> * findRek(const K &k);
  25.   bool inTable(const K &k)const;
  26.   template<class X, class Y>
  27.   friend ostream & operator <<(ostream &out, const Table<X,Y> &t);
  28.   ostream & display(ostream & out, Rek<K,D> **rt)const;
  29. };
  30.  
  31. template<class K, class D>
  32. Table<K,D>::Table(){
  33.   loc = 0;
  34.   size = 0;
  35.   root[loc] = new Rek<K,D>();   //line 41
  36.  
  37. }
  38.  
  39. template<class K, class D>
  40. Table<K,D>::Table(const Table &t){
  41.   loc += 1;
  42.   root[loc] = new Rek<K,D>();
  43.   copy(t.root,root);
  44. }
  45.  
  46.   template<class K, class D>
  47.   Table<K,D>::Table(int sz){
  48.     // int i = 0;
  49.     // for(int j = 0; j < sz; j++)
  50.     // root[j] = new Rek<K,D>();
  51.     loc = 0;
  52.     root[loc] = NULL;
  53.     size = sz;
  54.   }
  55.  
  56. template<class K, class D>
  57. Table<K,D>::~Table(){
  58.   for(int i = 0; i < size; i ++)
  59.     delete root[i];
  60. }
  61.  
  62. /*template<class K, class D>
  63. void Table<K,D>::kill(Rek<K,D> *&rt){
  64.   if(rt != NULL)
  65.     delete rt;
  66.     }*/
  67.  
  68. template<class K, class D>
  69. const Table<K,D> & Table<K,D>::operator =(const Table & t){
  70.   root = NULL;
  71.   copy(t.root, root);
  72.   return *this;
  73. }
  74.  
  75. template<class K, class D>
  76. void Table<K,D>::copy(Rek<K,D> **from, Rek<K,D> **&to){
  77.   if(from != NULL){
  78.     to[loc] = new Rek<K,D>(from[loc]->getKey(), from[loc]->data);
  79.     //for(int j = 0; j < size; j++)
  80.       //to[j] = new Rek<K,D>(from[j]->getKey(), from[j]->data);
  81.     check(to != NULL, "Heap overflow", __LINE__, __FILE__);
  82. }
  83. }
  84.  
  85. template<class K, class D>
  86. Table<K,D> &Table<K,D>::add(const K &k, const D &d){
  87.   add(k,d,root);
  88.   return *this;
  89. }
  90.  
  91. template<class K, class D>
  92. void Table<K,D>::add(const K &k, const D &d, Rek<K,D> **&rt){
  93.   if(rt == NULL){
  94.     //*rt = new Rek<K,D> (k,d);
  95.     rt[loc] = new Rek<K,D>(k,d);
  96.     check(rt != NULL, "Heap overflow", __LINE__, __FILE__);
  97.   }
  98.   else
  99.     check(false, "duplicate key", __LINE__, __FILE__);
  100. }
  101.  
  102. template<class K, class D>
  103. D & Table<K,D>::operator[](const K &k){
  104.   if(!inTable(k))
  105.     add(k);
  106.   return findRek(k)->data;
  107. }
  108.  
  109. template<class K, class D>
  110. const D & Table<K,D>::operator[](const K &k)const{
  111.   check(inTable(k), "Missing key", __LINE__, __FILE__);
  112.   return findRek(k)->data;
  113. }
  114.  
  115. template<class K, class D>
  116. Rek<K,D> *Table<K,D>::findRek(const K &k){
  117.   Rek<K,D> **temp;
  118.   temp = root;
  119.   int i = 0;
  120.   int j = 0;
  121.   while(temp[i][j].getKey() != k && i < loc){
  122.     temp[i][j] = root[i][j];
  123.     i++;
  124.   }
  125.   return temp[i];
  126. }
  127.   template<class K, class D>
  128. bool Table<K,D>::inTable(const K &k)const{
  129.   Rek<K,D> **temp;
  130.   temp = root;
  131.   int i = 0;
  132.   int j = 0;
  133.   while(temp[i][j].getKey() != k && i < loc){
  134.     temp[i][j] = root[i][j];
  135.     i++;
  136.   }
  137.   return temp != NULL;
  138. }
  139.  
  140. template<class X, class Y>
  141. ostream &operator<<(ostream &out, const Table<X,Y> &t){
  142.   return t.display(out, t.root);
  143. }
  144.  
  145.   template<class K, class D>
  146. ostream & Table<K,D>::display(ostream &out, Rek<K,D> **rt)const{
  147.   if(rt != NULL){
  148.     for(int j = 0; j < loc; j++)
  149.       out<<rt[j]<<endl;
  150.   }
  151.   return out;
  152.   }
  153.  
  154. #endif
and the Rek class as follows:

Expand|Select|Wrap|Line Numbers
  1. #ifndef REK_T
  2. #define REK_T
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. template <class K,class D>
  7. class Rek{
  8. private:
  9.   K key;
  10. public:
  11.   D data;
  12.   K getKey()const;
  13.   Rek();
  14.   Rek(const K &k, const D &d = D());
  15.   template<class X, class Y>
  16.   friend ostream &operator<<(ostream &out, const Rek<X,Y> &r);
  17. };
  18.  
  19. template<class X, class Y>
  20. X Rek<X,Y>::getKey()const{
  21.   return key;
  22. }
  23.  
  24. template<class K, class D>
  25. Rek<K,D>::Rek(){
  26.   key = new K();  //line 32
  27.   data = new D(); //line 33
  28. }
  29. template<class K, class D>
  30. Rek<K,D>::Rek(const K &k, const D &d):key(k),data(d){
  31. }
  32.  
  33. template<class X, class Y>
  34. ostream &operator<<(ostream &out, const Rek<X,Y> &r){
  35.   return out<<"{"<<r.key<<", "<<r.data<<"}"<<endl;
  36. }
  37.  
  38. #endif
Thank you
Mar 4 '10 #1
1 1399
Banfa
9,065 Recognized Expert Moderator Expert
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).
Mar 4 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6560
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
1849
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
4
1842
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
2
1486
by: Alex Drummond | last post by:
Hello, Is there any way of specializing a templated function on a type which is itself templated? Here's the simplest example of the problem I can think of. Say I have written an implementation...
6
1510
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
6
1298
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated...
2
1807
by: Amadeus W. M. | last post by:
I have a bunch of templated functions: template <class Type_t> double f2(Type_t x) { return 2*x; } template <class Type_t> double f3(Type_t x) { return 3*x; }
0
2326
by: Mike | last post by:
Hi. I can't figure out why a button's click event is not firing in a templated control I've created (first time I've tried creating one). Please can someone help? On a point of lesser importance,...
2
2909
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
2
2272
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.