943,544 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 910
  • C++ RSS
Jul 29th, 2008
0

Problem when using a template classes to store user-defined types

Expand Post »
Hallo everybody. I am a student trying to extend my knowledge in cPP while using UML and system modelling.
I created a "vector template class" using the rhapsody system modeling tool for storing different data types. I used the template to store int and float data types successfully using either composition or aggregation.

Now and in order to learn sth more I tried to use the template for storing my own defined data type of a class CNotebook which inherits from a CDesktopPC class.

Unfortunately it doesnt compile. (see lines 140-144 of CTesterClass) I suspect that I have to overload some operators but I am not sure where I should do that.

I attach you the UML diagram to get a rough idea of what I am trying exactly to do and both the vector template class and the CNotebook, CDesktopPC, CTesterClass and VectorOfNotebooks classes.

A. CDesktopPC class:
CPP Syntax (Toggle Plain Text)
  1. SPECIFICATION:
  2. ..........
  3. #include <iostream>
  4. #include <sstream>
  5.  
  6. //## class CDesktopPC
  7. class CDesktopPC {
  8. //// Constructors and destructors ////
  9. public :
  10. CDesktopPC(const std::string & man, const std::string proc, int ram, int disk);
  11. CDesktopPC();
  12. ~CDesktopPC();
  13. //// Operations ////
  14. int get_disk_size() const;
  15. std::string get_manufacturer() const;
  16. std::string get_processor() const;
  17. int get_ram_size() const;
  18. void set_disk_size(int ds);
  19. void set_manufacturer(const std::string manu);
  20. void set_processor(const std::string pro);
  21. void set_ram_size(int rs);
  22. virtual std::string to_string() const;
  23. //// Attributes ////
  24. private :
  25. int disk_size; //## attribute disk_size
  26. std::string manufacturer; //## attribute manufacturer
  27. std::string processor; //## attribute processor
  28. int ram_size; //## attribute ram_size
  29. };
  30. ...........
  31. IMPLEMENTATION:
  32. ...........
  33. #include "CDesktopPC.h"
  34.  
  35. CDesktopPC::CDesktopPC(const std::string & man, const std::string proc, int ram, int disk) : manufacturer(man),processor(proc),ram_size(ram),disk_size(disk) {}
  36. CDesktopPC::CDesktopPC() {}
  37. CDesktopPC::~CDesktopPC() { std::cout<<"Destructor of CDesktopPC."<<std::endl;}
  38. int CDesktopPC::get_disk_size() const {return disk_size;}
  39. std::string CDesktopPC::get_manufacturer() const { return manufacturer;}
  40. std::string CDesktopPC::get_processor() const {return processor;}
  41. int CDesktopPC::get_ram_size() const { return ram_size;}
  42. void CDesktopPC::set_disk_size(int ds) { disk_size=ds;}
  43. void CDesktopPC::set_manufacturer(const std::string manu) {manufacturer=manu;}
  44. void CDesktopPC::set_processor(const std::string pro) {processor=pro;}
  45. void CDesktopPC::set_ram_size(int rs) {ram_size=rs;}
  46. std::string CDesktopPC::to_string() const {
  47. //#[ operation to_string() const
  48. std::ostringstream sb;
  49. sb<<"manufacturer: "<<manufacturer
  50. <<"\nCPU: "<<processor
  51. <<"\nRAM: "<<ram_size<< " megabytes"
  52. <<"\nDisk: "<<disk_size<<" gigabytes";
  53. return sb.str();
  54. }

B. CNotebook class:
CPP Syntax (Toggle Plain Text)
  1. SPECIFICATION:
  2. .........
  3. #include "CDesktopPC.h"
  4.  
  5. class CNotebook : public CDesktopPC {
  6. //// Constructors and destructors ////
  7. public :
  8. CNotebook(const std::string & proc, int ram, int disk, double screen, double wei);
  9. CNotebook(const std::string & man, const std::string & proc, int ram, int disk, double screen, double wei);
  10. CNotebook();
  11. ~CNotebook();
  12. //// Operations ////
  13. double get_screen_size() const;
  14. double get_weight() const;
  15. void set_size(double sz);
  16. void set_weight(double we);
  17. std::string to_string() const;
  18. //// Attributes ////
  19. static const char * DEFAULT_LT_MAN; //## attribute DEFAULT_LT_MAN
  20. private :
  21. double screen_size; //## attribute screen_size
  22. double weight; //## attribute weight
  23. };
  24. .......
  25. IMPLEMENTATION:
  26. .......
  27. #include "CNotebook.h"
  28.  
  29. const char * CNotebook::DEFAULT_LT_MAN("H MARKA MOU");
  30. CNotebook::CNotebook(const std::string & proc, int ram, int disk, double screen, double wei) : CDesktopPC(DEFAULT_LT_MAN,proc,ram,disk),screen_size(screen),weight(wei) {}
  31. CNotebook::CNotebook(const std::string & man, const std::string & proc, int ram, int disk, double screen, double wei) : CDesktopPC(man,proc,ram,disk),screen_size(screen),weight(wei) {}
  32.  
  33. CNotebook::CNotebook() {}
  34. CNotebook::~CNotebook() {}
  35. double CNotebook::get_screen_size() const { return screen_size;}
  36. double CNotebook::get_weight() const { return weight;}
  37. void CNotebook::set_size(double sz) { screen_size=sz;}
  38. void CNotebook::set_weight(double we) { weight=we;}
  39. std::string CNotebook::to_string() const {
  40. std::ostringstream sb;
  41. sb<<CDesktopPC::to_string()
  42. <<"\nScreen size: "<<screen_size
  43. <<"\nWeight: "<<weight;
  44. return sb.str();
  45. }

C. CVectorTemplate class
CPP Syntax (Toggle Plain Text)
  1. SPECIFICATION:
  2. ........
  3. #include <stdexcept>
  4.  
  5. template <typename Item_Type>
  6. class CVectorTemplate {
  7. //// Constructors and destructors ////
  8. public :
  9. CVectorTemplate();
  10. virtual ~CVectorTemplate();
  11. //// Attributes ////
  12. private :
  13. static size_t INITIAL_CAPACITY; //## attribute INITIAL_CAPACITY
  14. size_t current_capacity; //## attribute current_capacity
  15. size_t num_items; //## attribute num_items
  16. Item_Type* the_data; //## attribute the_data
  17. public :
  18. void erase(size_t index); //Remove an entry based on its index
  19. void insert(size_t index, const Item_Type& the_value); // Insert an entry to the data inserting it before the item at the specified index.
  20. Item_Type& operator[](size_t index); //Get a value in the vector based on its index.
  21. const Item_Type& operator[](size_t index) const; // Get a constant value in the vector based on its index.
  22. void pop_back(); //Remove the last item in the vector.
  23. void push_back(const Item_Type& the_value); //Insert a new item at the end of the vector.
  24. void reserve(size_t new_capacity); //Ensure that the capacity is at least a given size.
  25. void swap(CVectorTemplate<Item_Type>& other); //Exchanges the contents of this vector with another.
  26. CVectorTemplate(const CVectorTemplate<Item_Type>& other); //Make a copy of a CVectorTemplate.
  27. size_t CVectorTemplatesize() const; // Get the current size of the vector
  28. Item_Type& back(); //Return a reference to the last item.
  29. const Item_Type& back() const; //Return a const reference to the last item.
  30. size_t capacity(); //Get the current capacity of the vector
  31. bool emptyCVectorTemplate() const; //Determine if the vector is empty
  32. Item_Type& front(); //Return a reference to the first item
  33. const Item_Type& front() const; //Return a const reference to the first item
  34. CVectorTemplate<Item_Type>& operator=(const CVectorTemplate<Item_Type>& other); // Assign the contents of one vector to another.
  35. };
  36. // ----------------------------------------------------------------------------
  37. // End of CVectorTemplate.h
  38. // ----------------------------------------------------------------------------
  39. template <typename Item_Type> size_t CVectorTemplate<Item_Type>::INITIAL_CAPACITY(10);
  40.  
  41. template <typename Item_Type> CVectorTemplate<Item_Type>::CVectorTemplate() : current_capacity(INITIAL_CAPACITY),the_data(new Item_Type[INITIAL_CAPACITY]),num_items(0) {}
  42.  
  43. template <typename Item_Type> CVectorTemplate<Item_Type>::CVectorTemplate(const CVectorTemplate<Item_Type>& other) : current_capacity(other.capacity),num_items(other.num_items),the_data(new Item_Type[other.current_capacity]) {
  44. for (size_t i=0;i<num_items;i++)
  45. the_data[i]=other.the_data[i]; }
  46.  
  47. template <typename Item_Type> CVectorTemplate<Item_Type>::~CVectorTemplate() {
  48. delete[] the_data; }
  49.  
  50. template <typename Item_Type> size_t CVectorTemplate<Item_Type>::CVectorTemplatesize() const { return num_items; }
  51.  
  52. template <typename Item_Type> Item_Type& CVectorTemplate<Item_Type>::back() {
  53. if (num_items == 0)
  54. throw std::out_of_range
  55. ("Attempt to access back of empty vector");
  56. return the_data[num_items-1]; }
  57.  
  58. template <typename Item_Type> const Item_Type& CVectorTemplate<Item_Type>::back() const {
  59. if (num_items == 0)
  60. throw std::out_of_range
  61. ("Attempt to access back of empty vector");
  62. return the_data[num_items-1]; }
  63.  
  64. template <typename Item_Type> size_t CVectorTemplate<Item_Type>::capacity() {
  65. return current_capacity; }
  66.  
  67. template <typename Item_Type> bool CVectorTemplate<Item_Type>::emptyCVectorTemplate() const {return num_items == 0;}
  68.  
  69. template <typename Item_Type> void CVectorTemplate<Item_Type>::erase(size_t index) { // Validate index.
  70. if (index >= num_items) {
  71. throw std::out_of_range
  72. ("index to erase is out of range"); }
  73. // Move items below the removed one up.
  74. for (size_t i = index + 1; i < num_items; i++) {
  75. the_data[i - 1] = the_data[i]; }
  76. num_items--; }
  77.  
  78. template <typename Item_Type> Item_Type& CVectorTemplate<Item_Type>::front() {
  79. if (num_items == 0)
  80. throw std::out_of_range
  81. ("Attempt to access front of empty vector");
  82. return the_data[0]; }
  83.  
  84. template <typename Item_Type> const Item_Type& CVectorTemplate<Item_Type>::front() const {
  85. if (num_items == 0)
  86. throw std::out_of_range
  87. ("Attempt to access front of empty vector");
  88. return the_data[0]; }
  89.  
  90. template <typename Item_Type> void CVectorTemplate<Item_Type>::insert(size_t index, const Item_Type& the_value) {
  91. // Validate index
  92. if (index > num_items) {
  93. throw std::out_of_range
  94. ("index to insert is out of range"); }
  95. // Ensure that there is space for the new item
  96. if (num_items == current_capacity) {
  97. reserve(2 * current_capacity); // allocate an expanded array }
  98. // Move data from index to num_items-1 down
  99. for (size_t i = num_items; i > index; i--) {
  100. the_data[i] = the_data[i - 1]; }
  101. // Insert the new item
  102. the_data[index] = the_value;
  103. num_items++; }
  104.  
  105. template <typename Item_Type> CVectorTemplate<Item_Type>& CVectorTemplate<Item_Type>::operator=(const CVectorTemplate<Item_Type>& other) {
  106. //Make a copy of the other CVectorTemplate object.
  107. CVectorTemplate<Item_Type> the_copy(other);
  108. //above the "deep copy constructor" was used
  109. //Now we swap the contents of self with the copy(the_copy) we made before.
  110. swap(the_copy);
  111. //finally we return the object. Upon return the copy will be destroyed.
  112. return *this; }
  113.  
  114. template <typename Item_Type> Item_Type& CVectorTemplate<Item_Type>::operator[](size_t index) {
  115. // Verify that the index is legal
  116. if (index >= num_items) {
  117. throw std::out_of_range
  118. ("index to operator[] is out of range"); }
  119. return the_data[index]; }
  120.  
  121. template <typename Item_Type> const Item_Type& CVectorTemplate<Item_Type>::operator[](size_t index) const {
  122. // Verify that the index is legal
  123. if (index >= num_items) {
  124. throw std::out_of_range
  125. ("index to operator[] is out of range"); }
  126. return the_data[index]; }
  127.  
  128. template <typename Item_Type> void CVectorTemplate<Item_Type>::pop_back() {
  129. num_items--; }
  130.  
  131. template <typename Item_Type> void CVectorTemplate<Item_Type>::push_back(const Item_Type& the_value) {
  132. // Make sure there is space for the new item.
  133. if (num_items == current_capacity) {
  134. reserve(2 * current_capacity); // Allocate an expanded array }
  135. // Insert the new item.
  136. the_data[num_items] = the_value;
  137. num_items++; }
  138.  
  139. template <typename Item_Type> void CVectorTemplate<Item_Type>::reserve(size_t new_capacity) {
  140. if (new_capacity > current_capacity) {
  141. if (new_capacity > 2 * current_capacity)
  142. current_capacity = new_capacity;
  143. else
  144. current_capacity *= 2; // Double the capacity.
  145.  
  146. Item_Type* new_data = new Item_Type[current_capacity];
  147. // Copy the data over
  148. for (size_t i = 0; i < num_items; i++)
  149. new_data[i] = the_data[i];
  150. // Free the memory occupied by the old copy.
  151. delete[] the_data;
  152. // Now point to the new data
  153. the_data = new_data;
  154. }
  155. }
  156.  
  157. template <typename Item_Type> void CVectorTemplate<Item_Type>::swap(CVectorTemplate<Item_Type>& other) {
  158. std::swap(num_items, other.num_items);
  159. std::swap(current_capacity, other.current_capacity);
  160. std::swap(the_data, other.the_data);
  161. }
  162. ........
D. CTesterClass class
CPP Syntax (Toggle Plain Text)
  1. SPECIFICATION:
  2. ........
  3. #include "CNotebook.h" //## dependency CNotebook
  4. #include "vect1int.h" //## link itsVect1int
  5. #include "vect2int.h" //## link itsVect2int
  6. #include "vect3float.h" //## link itsVect3float
  7. class VectorOfNotebooks; //## link itsVectorOfNotebooks
  8. class bindingclass; //## link itsBindingclass
  9.  
  10. class CTesterClass {
  11. //// Constructors and destructors ////
  12. public :
  13. CTesterClass();
  14. ~CTesterClass();
  15. //// Operations ////
  16. //## operation testCVectorTemplateClass()
  17. void testCVectorTemplateClass();
  18. //// Additional operations ////
  19. //## auto_generated
  20. bindingclass* getItsBindingclass() const;
  21. //## auto_generated
  22. void setItsBindingclass(bindingclass* p_bindingclass);
  23. //## auto_generated
  24. vect1int* getItsVect1int() const;
  25. //## auto_generated
  26. vect2int* getItsVect2int() const;
  27. //## auto_generated
  28. vect3float* getItsVect3float() const;
  29. //## auto_generated
  30. VectorOfNotebooks* getItsVectorOfNotebooks() const;
  31. //## auto_generated
  32. void setItsVectorOfNotebooks(VectorOfNotebooks* p_VectorOfNotebooks);
  33. protected :
  34. //## auto_generated
  35. void cleanUpRelations();
  36. //// Relations and components ////
  37. bindingclass* itsBindingclass; //## link itsBindingclass
  38. vect1int itsVect1int; //## link itsVect1int
  39. vect2int itsVect2int; //## link itsVect2int
  40. vect3float itsVect3float; //## link itsVect3float
  41. VectorOfNotebooks* itsVectorOfNotebooks; //## link itsVectorOfNotebooks
  42. };
  43. ........
  44. IMPLEMENTATION:
  45. ........
  46. #include "CTesterClass.h"
  47. #include "bindingclass.h" //## link itsBindingclass
  48. #include "VectorOfNotebooks.h" //## link itsVectorOfNotebooks
  49.  
  50. CTesterClass::CTesterClass() {
  51. itsBindingclass = NULL;
  52. itsVectorOfNotebooks = NULL;
  53. testCVectorTemplateClass(); }
  54.  
  55. CTesterClass::~CTesterClass() {
  56. cleanUpRelations(); }
  57.  
  58. void CTesterClass::testCVectorTemplateClass() {
  59. vect1int vect1;
  60. //gemise ton vect1 me 20 stoixeia ta poia periexoun tous akeraious 0 ws 19
  61. for (int i = 0; i < 20; i++) {
  62. vect1.push_back(i); }
  63. bool pass1 = true;
  64. //tsekare to index tou vect1
  65. for (int i = 0; i < 20; i++) {
  66. if (i != vect1[i]) {
  67. std::cout << i << " != vect1[" << i << "] == "<< vect1[i] << '\n';
  68. pass1 = false; }
  69. }
  70. if (pass1)
  71. std::cout << "Index test passed\n";
  72.  
  73. std::cout<<"the last item of vect1int vect1 is" <<vect1.back()<<std::endl;
  74. std::cout<<"the first item of vect1int vect1 is" <<vect1.front()<<std::endl;
  75.  
  76. //Check Out of range negative index
  77. bool passed1b = false;
  78. try {
  79. vect1[-1] = 0;
  80. } catch (std::out_of_range& ex) {
  81. passed1b = true;
  82. std::cout << "Out of range negative index test passed\n";
  83. }
  84. if (!passed1b)
  85. std::cout << "Out of range negative index test failed\n";
  86.  
  87. //check Out of range index too large
  88. bool passed1c = false;
  89. try {
  90. vect1[vect1.CVectorTemplatesize()] = 0;
  91. } catch (std::out_of_range& ex) {
  92. passed1c = true;
  93. std:: cout << "Out of range index too large test passed\n";
  94. }
  95. if (!passed1c)
  96. std::cout << "Out of range index too large test failed\n";
  97. ///////////______________________________________________///////////////
  98. vect2int vect2;
  99. //gemise ton vect2 me 20 stoixeia ta poia einai:0,2,4,6,8,10,12,14,16,18
  100. for (int i = 0; i < 20; i += 2) {
  101. vect2.push_back(i); }
  102. for (int i = 0; i < 20; i += 2) {
  103. vect2.insert(i+1, i+1); }
  104.  
  105. bool pass2 = true;
  106. //tsekare to index tou vect
  107. for (int i = 0; i < 20; i++) {
  108. if (i != vect2[i]) {
  109. std::cout << i << " != vect2[" << i << "] == "<< vect2[i] << '\n';
  110. pass2 = false;
  111. }
  112. }
  113. if (pass2)
  114. std::cout << "Insert test passed\n";
  115.  
  116. std::cout<<"the last item of vect2int vect2 is" <<vect2.back()<<std::endl;
  117. std::cout<<"the first item of vect2int vect2 is" <<vect2.front()<<std::endl;
  118. ///////////______________________________________________///////////////
  119. vect3float vect3;
  120.  
  121. //gemise ton vect3 me 20 stoixeia ta poia einai:0.0,2.0,4.0,6.0,8.0,10.0,12.0,14.0,16.0,18.0
  122. for (float i = 0.0; i < 20.0; i += 2.0) {
  123. vect3.push_back(i);
  124. }
  125. vect3.pop_back();
  126. std::cout<<"the last item of vectfloat vect3 is" <<vect3.back()<<std::endl;
  127. ///////////______________________________________________///////////////
  128. itsBindingclass= new bindingclass;
  129.  
  130. //gemise ton vect4 me 20 stoixeia ta poia einai:0,2,4,6,8,10,12,14,16,18
  131. for (int i = 0; i < 20; i += 2) {
  132. itsBindingclass->push_back(i);
  133. }
  134. itsBindingclass->pop_back();
  135. itsBindingclass->pop_back();
  136. std::cout<<"the last item of vectfloat vect4 is" <<itsBindingclass->back()<<std::endl;
  137. ///////////______________________________________________///////////////
  138. itsVectorOfNotebooks = new VectorOfNotebooks;
  139.  
  140. ///--------------///MY PROBLEM: ///--------------//
  141. ///////////////all next three statements cause a compile error
  142. //itsVectorOfNotebook->insert(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));
  143. //itsVectorOfNotebook->insert(CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));
  144. //itsVectorOfNotebook.insert(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));
  145.  
  146. ///////////______________________________________________///////////////
  147. ///////////______________________________________________///////////////
  148.  
  149. //CDesktopPC c1;
  150. CDesktopPC Mycomputer("HP", "Intel P4", 2028, 120);
  151.  
  152. //CNotebook c3("Ace", "AMD Athlon 2000", 512, 60);
  153. CNotebook YourPC("Ace", "AMD Athlon 2000", 1024, 100, 15.5, 7.5);
  154.  
  155. CNotebook HerPC("intel II", 128, 20, 17.0, 12.5);
  156. //cout << c2.manufacturer << ", " << c2.processor << endl;
  157.  
  158. std::cout << Mycomputer.get_disk_size() << ", " << YourPC.get_ram_size() << std::endl;
  159.  
  160. std::cout <<"My Computer is:\n "<< Mycomputer.to_string() <<std::endl;
  161. std::cout <<"Your Pc is:\n "<< YourPC.to_string() << std::endl;
  162. std::cout <<"Her Pc is:\n "<< HerPC.to_string() << std::endl;
  163.  
  164. //sel196 virtual functions
  165. CDesktopPC* PC1;
  166. CDesktopPC* Laptop1;
  167.  
  168. PC1= new CDesktopPC ("Asus","AMD3",256,40);
  169. Laptop1= new CNotebook ("Fujitsu","intel 3",1000,60,15.4,6.0);
  170.  
  171. std::cout <<"PC1 is:\n "<< PC1->to_string() << std::endl;
  172. std::cout <<"Laptop1 is:\n "<< Laptop1->to_string() << std::endl;
  173. ///////////______________________________________________///////////////
  174. bindingclass* CTesterClass::getItsBindingclass() const {
  175. return itsBindingclass;}
  176. void CTesterClass::setItsBindingclass(bindingclass* p_bindingclass) {
  177. itsBindingclass = p_bindingclass;}
  178. vect1int* CTesterClass::getItsVect1int() const {
  179. return (vect1int*) &itsVect1int;}
  180. vect2int* CTesterClass::getItsVect2int() const {
  181. return (vect2int*) &itsVect2int;}
  182. vect3float* CTesterClass::getItsVect3float() const {
  183. return (vect3float*) &itsVect3float;}
  184. VectorOfNotebooks* CTesterClass::getItsVectorOfNotebooks() const {
  185. return itsVectorOfNotebooks;}
  186. void CTesterClass::setItsVectorOfNotebooks(VectorOfNotebooks* p_VectorOfNotebooks) {
  187. itsVectorOfNotebooks = p_VectorOfNotebooks;
  188. }
  189.  
  190. void CTesterClass::cleanUpRelations() {
  191. if(itsBindingclass != NULL)
  192. {
  193. itsBindingclass = NULL;
  194. }
  195. if(itsVectorOfNotebooks != NULL)
  196. {
  197. itsVectorOfNotebooks = NULL;
  198. }
  199. }

E. VectorOfNotebooks class
CPP Syntax (Toggle Plain Text)
  1. SPECIFICATION:
  2. ......
  3. #include "CNotebook.h" //## dependency CNotebook
  4. #include "CVectorTemplate.h" //## class VectorOfNotebooks
  5. class VectorOfNotebooks : public CVectorTemplate<CNotebook> {
  6. //// Constructors and destructors ////
  7. public :
  8. VectorOfNotebooks();
  9. ~VectorOfNotebooks();
  10. };
  11. ......
  12. IMPLEMENTATION:
  13. ......
  14. #include "VectorOfNotebooks.h"
  15. VectorOfNotebooks::VectorOfNotebooks() {}
  16. VectorOfNotebooks::~VectorOfNotebooks() {}
  17. ......

Thanks a lot in advance everybody & sorry for the amount of code.
Please feel to free to tell me if u see any bad programming practice.
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Jul 29th, 2008
0

Re: Problem when using a template classes to store user-defined types

The only insert function of the vector template I found takes two parameters - one of type size_t, and the other of type Item_type. When you call it, you're only specifying the Item_type parameter.

Also, I believe the entire purpose of the VectorOfNotebooks class is to not have to use the template directly. There is no added functionality. Use typdefs instead.
C++ Syntax (Toggle Plain Text)
  1. typedef CVectorTemplate<CNotebook> VectorOfNotebooks;
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 29th, 2008
0

Re: Problem when using a template classes to store user-defined types

Yes you are right that i forgot the index. However it still does not compile.

More specifically:

Everything compiles and runs succesfully whenever I use only the statement:
CPP Syntax (Toggle Plain Text)
  1. itsVectorOfNotebooks = new VectorOfNotebooks;

However whenever I add any of the following statements so that I add some Notebook objects into the vector data structure comilation fails:
CPP Syntax (Toggle Plain Text)
  1. itsVectorOfNotebook->push_back(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));
  2. itsVectorOfNotebook.push_back(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));
  3. itsVectorOfNotebook->insert(0,new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));
  4. itsVectorOfNotebook->insert(0,CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));

The compile error I get is:
C++ Syntax (Toggle Plain Text)
  1. Building ------------ CVectorTemplateComponent.exe ------------
  2. Executing: "C:\aThesisEnv\Telelogic\Rhapsody 7.2\Share\etc\mingwmake.bat" CVectorTemplateComponent.mak build
  3. Setting environment for MinGW
  4. "make.exe"
  5. Compiling CTesterClass.cpp
  6. CTesterClass.cpp: In member function `void CTesterClass::testCVectorTemplateClass()':
  7. CTesterClass.cpp:68: warning: passing negative value `-0x000000001' for converting 1 of `Item_Type& CVectorTemplate<Item_Type>::operator[](size_t) [with Item_Type = int]'
  8. CTesterClass.cpp:135: error: `itsVectorOfNotebook' undeclared (first use this function)
  9. CTesterClass.cpp:135: error: (Each undeclared identifier is reported only once for each function it appears in.)
  10. make: *** [CTesterClass.o] Error 1
  11. Build Done

Note:
I develop the code through rhapsody trying to model everything the same time in UML 2.0. Therefore I used these 2 extra classes: CTesterClass and VectorOfNotebooks.

Thanks again everybody
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Jul 29th, 2008
0

Re: Problem when using a template classes to store user-defined types

@second error
you wrote itsVectorOfNotebook in one or more places (one of which is line 135 of CTesterClass.cpp) where you should have written itsVectorOfNotebooks .

Could you specify which line is 68 in CTestClass.cpp? The line numbers are different since you bundled .h and .cpp files.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 29th, 2008
0

Re: Problem when using a template classes to store user-defined types

Thanks a lot. At least you know I got enough tired.... trying to implement this, that I forgot an s......

I added the forgoten s, and removed the new operator:

CPP Syntax (Toggle Plain Text)
  1. itsVectorOfNotebooks = new VectorOfNotebooks;
  2. itsVectorOfNotebooks->push_back(CNotebook("HP nwer", "Intel P9", 2048, 120,15.4,4.0));
  3. itsVectorOfNotebooks->insert(0,CNotebook("HP nwer2", "Intel P10", 2048, 340,15.4,3.0));

and there are nor compilation nor linking errors.

Two more questions:
1.What about the rest of a code? Do u think there are other things that if changed would improve the code greatly??

2.Of what else should I be careful when using my own data structures to store my other user defined data types?

Thanks a lot again
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Jul 29th, 2008
0

Re: Problem when using a template classes to store user-defined types

Sorry I forgot to answer you your question concerning the warning.
The line 68 you asked is in:

CPP Syntax (Toggle Plain Text)
  1. .......
  2. try {
  3. 68: vect1[-1] = 0;
  4. } catch
  5. ........

By the way how can I call now the method: to_string()
for the object I created before through:
CPP Syntax (Toggle Plain Text)
  1. VectorOfNotebooks = new VectorOfNotebooks;
  2. itsVectorOfNotebooks->push_back(CNotebook("HP nwer", "Intel P9", 2048, 120,15.4,4.0));
  3. itsVectorOfNotebooks->insert(0,CNotebook("HP nwer2", "Intel P10", 2048, 340,15.4,3.0));
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Graphics API's
Next Thread in C++ Forum Timeline: thread abort





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC