Class Template Problem - Constructor Issues - Please help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 5
Reputation: doublediamond is an unknown quantity at this point 
Solved Threads: 1
doublediamond doublediamond is offline Offline
Newbie Poster

Class Template Problem - Constructor Issues - Please help

 
0
  #1
Mar 27th, 2008
Hey guys, new poster here. I'm rather desperate here, I just do not understand the concept of templates very well. I've used the search function, but I don't see the problem addressed in any other threads. Any help is much appreciated.

My assignment is to create a templated vector class using the following prototypes/definitions:
//I cannot modify these prototypes at all.
  1. #ifndef _MYVECTOR
  2. #define _MYVECTOR
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class BADINDEX{};
  7.  
  8. template <class T>
  9. class containerInterface
  10. {
  11. public:
  12. virtual containerInterface <T>& pushFront(T) = 0;
  13. virtual containerInterface <T>& pushBack(T) = 0;
  14. virtual containerInterface <T>& popFront(T&) throw(BADINDEX) = 0;
  15. virtual containerInterface <T>& popBack(T&) throw(BADINDEX) = 0;
  16. virtual int getSize() = 0;
  17. virtual bool isFull() = 0;
  18. virtual bool isEmpty() = 0;
  19. virtual T front() throw(BADINDEX) = 0;
  20. virtual T back() throw(BADINDEX) = 0;
  21. virtual T& operator[] (int) throw(BADINDEX) = 0;
  22. virtual void erase() throw(BADINDEX) = 0;
  23. };
  24.  
  25. template <class T>
  26. class myVector:public containerInterface<T>
  27. {
  28. public:
  29. myVector();
  30. ~myVector();
  31. myVector(const myVector&);
  32. myVector <T>& operator=(myVector&);
  33. myVector <T>& pushFront(T);
  34. myVector <T>& pushBack (T);
  35. myVector <T>& popFront (T&) throw(BADINDEX);
  36. myVector <T>& popBack (T&) throw(BADINDEX);
  37. T front() throw(BADINDEX);
  38. T back() throw(BADINDEX);
  39. T& operator[] (int) throw(BADINDEX);
  40. int getSize();
  41. bool isFull();
  42. bool isEmpty();
  43. void erase();
  44.  
  45. private:
  46. T *data;
  47. int size;
  48. int capacity;
  49. void grow();
  50. void shiftRight();
  51. void shiftLeft();
  52. };

I think I understand how to do it, and have written all the functions, but it will not compile, and I think my constructor, copy constructor, and destructor are the primary culprits, since they do not have a bracketed <T> as part of the function header. Here are the functions I've written:

  1. myVector::myVector()
  2. {
  3. size = 0;
  4. capacity = 10;
  5.  
  6. data = new T [capacity];
  7.  
  8. }
  9.  
  10. myVector::~myVector()
  11. {
  12. delete [] data;
  13. }
  14.  
  15. myVector::myVector(const myVector& other)
  16. {
  17. size = other.size;
  18. capacity = other.capacity;
  19.  
  20. data = new T [capacity];
  21.  
  22. for(int i = 0; i < size; i++)
  23. {
  24. data[i] = other.data[i];
  25. }
  26. }
  27.  
  28. myVector<T>& myVector::operator=(myVector& other)
  29. {
  30. if(data != other.data)
  31. {
  32. delete[] data;
  33. size = other.size;
  34. capacity = other.capacity;
  35.  
  36. data = new T [capacity];
  37.  
  38. for(int i = 0; i < size; i++)
  39. {
  40. data[i] = other.data[i];
  41. }
  42. }
  43.  
  44. return *this;
  45. }
  46.  
  47. myVector<T>& myVector::pushFront(T n)
  48. {
  49. shiftRight();
  50. data[0] = n;
  51.  
  52. return *this;
  53. }
  54.  
  55. myVector<T>& myVector::pushBack(T n)
  56. {
  57. size++;
  58. grow();
  59.  
  60. data[size] = n;
  61.  
  62. return *this;
  63. }
  64.  
  65. myVector<T>& myVector::popFront(T& n)
  66. {
  67. n = data[0];
  68.  
  69. shiftLeft();
  70.  
  71. return *this;
  72. }
  73.  
  74. myVector<T>& myVector::popBack(T& n)
  75. {
  76. n = data[size];
  77.  
  78. size--;
  79.  
  80. return *this;
  81. }
  82.  
  83. T front()
  84. {
  85. return data[0];
  86. }
  87.  
  88. T back()
  89. {
  90. return data[size];
  91. }
  92.  
  93. T& operator[](int n)
  94. {
  95. return data[n];
  96. }
  97.  
  98. int getSize()
  99. {
  100. return size;
  101. }
  102.  
  103. bool isFull()
  104. {
  105. return (size == capacity);
  106. }
  107.  
  108. bool isEmpty()
  109. {
  110. bool answer = false;
  111.  
  112. if(size == 0)
  113. {
  114. answer = true;
  115. }
  116.  
  117. return answer;
  118. }
  119.  
  120. void erase()
  121. {
  122. delete []data;
  123.  
  124. size = 0;
  125. capacity = 10;
  126.  
  127. data = new T [capacity];
  128. }
  129.  
  130. void grow()
  131. {
  132. while(size >= capacity)
  133. {
  134. T* temp;
  135. temp = data;
  136.  
  137. capacity *= 2;
  138.  
  139. data = new T[capacity];
  140.  
  141. for(int i = 0; i < size; i++)
  142. {
  143. data[i] = temp.data[i];
  144. }
  145.  
  146. delete []temp;
  147. }
  148.  
  149. }
  150.  
  151. void shiftRight()
  152. {
  153. size++;
  154. grow();
  155.  
  156. for(int i = size; i > 0; i--)
  157. {
  158. data[i] = data[i - 1];
  159. }
  160. }
  161.  
  162. void shiftLeft()
  163. {
  164. size--;
  165.  
  166. for(int i = 0; i < size; i++)
  167. {
  168. data[i] = data[i + 1];
  169. }
  170. }


I get a compile error on the line of the constructor telling me that I can't use the template class myVector without template parameters. This makes sense, but I don't know how to fix it. Any help is appreciated. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,617
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Class Template Problem - Constructor Issues - Please help

 
0
  #2
Mar 27th, 2008
If you define a member function for a template class outside of the class definition, you need to recreate the template parameters:
  1. template <typename T>
  2. myVector<T>::myVector()
  3. {
  4. size = 0;
  5. capacity = 10;
  6.  
  7. data = new T [capacity];
  8.  
  9. }
  10.  
  11. template <typename T>
  12. myVector<T>::~myVector()
  13. {
  14. delete [] data;
  15. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: doublediamond is an unknown quantity at this point 
Solved Threads: 1
doublediamond doublediamond is offline Offline
Newbie Poster

Re: Class Template Problem - Constructor Issues - Please help

 
0
  #3
Mar 27th, 2008
Thanks, Narue, that was helpful. That solved most of my problems, and I figured out some of the exception handling issues that I had as well. I think something is still fundamentally wrong, as I am getting the following errors.

  1.  
  2. #ifndef _MYVECTOR
  3. #define _MYVECTOR
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class BADINDEX{};
  8.  
  9. template <class T>
  10. class containerInterface
  11. {
  12. public:
  13. virtual containerInterface <T>& pushFront(T) = 0;
  14. virtual containerInterface <T>& pushBack(T) = 0;
  15. virtual containerInterface <T>& popFront(T&) throw(BADINDEX) = 0;
  16. virtual containerInterface <T>& popBack(T&) throw(BADINDEX) = 0;
  17. virtual int getSize() = 0;
  18. virtual bool isFull() = 0;
  19. virtual bool isEmpty() = 0;
  20. virtual T front() throw(BADINDEX) = 0;
  21. virtual T back() throw(BADINDEX) = 0;
  22. virtual T& operator[] (int) throw(BADINDEX) = 0;
  23. virtual void erase() = 0;
  24. };
  25.  
  26. template <class T>
  27. class myVector:public containerInterface<T>
  28. {
  29. public:
  30. myVector();
  31. ~myVector();
  32. myVector(const myVector&);
  33. myVector <T>& operator=(myVector&);
  34. myVector <T>& pushFront(T);
  35. myVector <T>& pushBack (T);
  36. myVector <T>& popFront (T&) throw(BADINDEX);
  37. myVector <T>& popBack (T&) throw(BADINDEX);
  38. T front() throw(BADINDEX);
  39. T back() throw(BADINDEX);
  40. T& operator[] (int) throw(BADINDEX);
  41. int getSize();
  42. bool isFull();
  43. bool isEmpty();
  44. void erase();
  45.  
  46. private:
  47. T *data;
  48. int size;
  49. int capacity;
  50. void grow();
  51. void shiftRight();
  52. void shiftLeft();
  53. };
  54.  
  55. template <class T>
  56. myVector<T>::myVector()
  57. {
  58. size = 0;
  59. capacity = 10;
  60.  
  61. data = new T [capacity];
  62.  
  63. }
  64.  
  65. template <class T>
  66. myVector<T>::~myVector()
  67. {
  68. delete [] data;
  69. }
  70.  
  71. template <class T>
  72. myVector<T>::myVector(const myVector& other)
  73. {
  74. size = other.size;
  75. capacity = other.capacity;
  76.  
  77. data = new T [capacity];
  78.  
  79. for(int i = 0; i < size; i++)
  80. {
  81. data[i] = other.data[i];
  82. }
  83. }
  84.  
  85. template <class T>
  86. myVector<T>& myVector<T>::operator=(myVector& other)
  87. {
  88. if(data != other.data)
  89. {
  90. delete[] data;
  91. size = other.size;
  92. capacity = other.capacity;
  93.  
  94. data = new T [capacity];
  95.  
  96. for(int i = 0; i < size; i++)
  97. {
  98. data[i] = other.data[i];
  99. }
  100. }
  101.  
  102. return *this;
  103. }
  104.  
  105. template <class T>
  106. myVector<T>& myVector<T>::pushFront(T n)
  107. {
  108. shiftRight();
  109. data[0] = n;
  110.  
  111. return *this;
  112. }
  113.  
  114. template <class T>
  115. myVector<T>& myVector<T>::pushBack(T n)
  116. {
  117. size++;
  118. grow();
  119.  
  120. data[size] = n;
  121.  
  122. return *this;
  123. }
  124.  
  125. template <class T>
  126. myVector<T>& myVector<T>::popFront(T& n) throw(BADINDEX)
  127. {
  128. if(size > 0)
  129. {
  130. n = data[0];
  131.  
  132. shiftLeft();
  133. }
  134. else
  135. {
  136. throw BADINDEX();
  137. }
  138. return *this;
  139. }
  140.  
  141. template <class T>
  142. myVector<T>& myVector<T>::popBack(T& n) throw(BADINDEX)
  143. {
  144.  
  145. if(size > 0)
  146. {
  147. n = data[size];
  148.  
  149. size--;
  150. }
  151. else
  152. {
  153. throw BADINDEX();
  154. }
  155. return *this;
  156. }
  157.  
  158. template <class T>
  159. T myVector<T>::front() throw(BADINDEX)
  160. {
  161. if(size > 0)
  162. {
  163. return data[0];
  164. }
  165. else
  166. {
  167. throw BADINDEX();
  168. }
  169. }
  170.  
  171. template <class T>
  172. T myVector<T>::back() throw(BADINDEX)
  173. {
  174. if(size > 0)
  175. {
  176. return data[size];
  177. }
  178. else
  179. {
  180. throw BADINDEX();
  181. }
  182. }
  183.  
  184. template <class T>
  185. T& myVector<T>::operator[](int n) throw(BADINDEX)
  186. {
  187. if(n >= capacity || n < 0)
  188. {
  189. throw BADINDEX();
  190. }
  191. else
  192. {
  193. return data[n];
  194. }
  195. }
  196.  
  197. template <class T>
  198. int myVector<T>::getSize()
  199. {
  200. return size;
  201. }
  202.  
  203. template <class T>
  204. bool myVector<T>::isFull()
  205. {
  206. return (size == capacity);
  207. }
  208.  
  209. template <class T>
  210. bool myVector<T>::isEmpty()
  211. {
  212. bool answer = false;
  213.  
  214. if(size == 0)
  215. {
  216. answer = true;
  217. }
  218.  
  219. return answer;
  220. }
  221.  
  222. template <class T>
  223. void myVector<T>::erase()
  224. {
  225. delete []data;
  226.  
  227. size = 0;
  228. capacity = 10;
  229.  
  230. data = new T [capacity];
  231. }
  232.  
  233. template <class T>
  234. void myVector<T>::grow()
  235. {
  236. while(size >= capacity)
  237. {
  238. T* temp;
  239. temp = data;
  240.  
  241. capacity *= 2;
  242.  
  243. data = new T[capacity];
  244.  
  245. for(int i = 0; i < size; i++)
  246. {
  247. data[i] = temp.data[i];
  248. }
  249.  
  250. delete []temp;
  251. }
  252.  
  253. }
  254.  
  255. template <class T>
  256. void myVector<T>::shiftRight()
  257. {
  258. size++;
  259. grow();
  260.  
  261. for(int i = size; i > 0; i--)
  262. {
  263. data[i] = data[i - 1];
  264. }
  265. }
  266.  
  267. template <class T>
  268. void myVector<T>::shiftLeft()
  269. {
  270. size--;
  271.  
  272. for(int i = 0; i < size; i++)
  273. {
  274. data[i] = data[i + 1];
  275. }
  276. }
  277.  
  278. #endif

I get the following errors:
Click image for larger version

Name:	problem.JPG
Views:	3
Size:	117.2 KB
ID:	5604

What it refers to as line 127 is line 118 in what I've pasted here, and what it refers to as line 256 is line 247 here.


The line it is referring to in the driver is:
  1. for(int i = 0; i < 10; i++)
  2. v1.pushBack(i).pushFront(-i);

Please advise. Thanks again. I really have no idea what those errors mean.
Last edited by doublediamond; Mar 27th, 2008 at 10:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Class Template Problem - Constructor Issues - Please help

 
0
  #4
Mar 28th, 2008
what it actually does.
Let me tell you what it actually does, and what it should do
first what it does
  1. template <class T>
  2. void myVector<T>::grow(){
  3. while(size >= capacity) {
  4. T* temp;
  5. temp = data;
  6. capacity *= 2;
  7. data = new T[capacity];
  8. for(int i = 0; i < size; i++)
  9. {
  10. data[i] = temp.data[i];
  11. }
  12. delete []temp;
  13. }
  14. }

basically T refers to int in your case now the new code becomes.
  1. void myVector<int>::grow(){
  2. while(size >= capacity) {
  3. int* temp;
  4. temp = data;
  5. capacity *= 2;
  6. data = new int[capacity];
  7. for(int i = 0; i < size; i++)
  8. {
  9. data[i] = temp.data[i];
  10. }
  11. delete []temp;
  12. }
  13. }
lets move from bottom up manner you'll recognise yourself.
what does your sentence data[i] = temp.data[i] means ???
int is a primitive type not a aggregate type, so a compile error is there.
now you know what should be change.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: doublediamond is an unknown quantity at this point 
Solved Threads: 1
doublediamond doublediamond is offline Offline
Newbie Poster

Re: Class Template Problem - Constructor Issues - Please help

 
0
  #5
Mar 28th, 2008
Yeah, I found that last night, thanks. temp is a pointer, not an object, so I just needed to change temp.data[i] to temp [i] and it worked.

I have some sort of small logic error with popback and popfront, but everything else works fine.

Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: doublediamond is an unknown quantity at this point 
Solved Threads: 1
doublediamond doublediamond is offline Offline
Newbie Poster

Re: Class Template Problem - Constructor Issues - Please help

 
0
  #6
Mar 28th, 2008
Figured out my last logic error. Solved. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC