Strange Template Instation Error or something

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster

Strange Template Instation Error or something

 
0
  #1
Oct 27th, 2009
I have the following driver for my template class the parameter being passed to the template is another class, the error I get is


Error line 28 : templateclass.h std::ostream<<ballteam is illegal
driver.cpp line 30 Where instationating templateclass<ballTeam>::print const ()

driver
  1. #include <iostream>
  2. #include <fstream>
  3. #include "arrayListTypeT.h"
  4. #include "ballTeam.h"
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. arrayListTypeT<ballTeam> teamList(10);
  11. ballTeam team;
  12. fstream fin;
  13.  
  14. fin.open("teamStats.data");
  15.  
  16. if(!fin.is_open())
  17. cout<<"Error: File does not exist or error opening file\n"<<endl;
  18.  
  19. while(!fin.eof())
  20. {
  21. fin>>team;
  22. teamList.insert(team);
  23.  
  24.  
  25. }
  26.  
  27.  
  28. fin.close();
  29.  
  30. teamList.print();
  31.  
  32. return 0;
  33. }

Class I am passing to template as parameter
  1. #include <iostream>
  2. #include "ballTeam.h"
  3. using namespace std;
  4.  
  5. ballTeam::ballTeam()
  6. {
  7. name = "";
  8. wins = 0;
  9. losses = 0;
  10. totalPoints = 0;
  11. }
  12.  
  13. ballTeam::ballTeam(string tName, int tWins, int tLosses, int tTotal)
  14. {
  15. name = tName;
  16. wins = tWins;
  17. losses = tLosses;
  18. totalPoints = tTotal;
  19. }
  20.  
  21. void ballTeam::set(string tName, int tWins, int tLosses, int tTotal)
  22. {
  23. name = tName;
  24. wins = tWins;
  25. losses = tLosses;
  26. totalPoints = tTotal;
  27. }
  28.  
  29. string ballTeam::getName() const
  30. {
  31. return name;
  32. }
  33.  
  34. int ballTeam::getWins() const
  35. {
  36. return wins;
  37. }
  38.  
  39. int ballTeam::getLosses() const
  40. {
  41. return losses;
  42. }
  43.  
  44. int ballTeam::getTotal() const
  45. {
  46. return totalPoints;
  47. }
  48.  
  49. bool ballTeam::operator==(const ballTeam &team)
  50. {
  51. float ratio;
  52. float teamRatio;
  53. ratio = (wins/ (float)(wins + losses));
  54. teamRatio = (team.wins/ (float)(team.wins + team.losses));
  55.  
  56. return (ratio == teamRatio);
  57. }
  58.  
  59. bool ballTeam::operator>(const ballTeam &team)
  60. {
  61. float ratio;
  62. float teamRatio;
  63. ratio = (wins/ (float)(wins + losses));
  64. teamRatio = (team.wins/ (float)(team.wins + team.losses));
  65.  
  66. return(ratio > teamRatio);
  67. }
  68.  
  69. bool ballTeam::operator<(const ballTeam &team)
  70. {
  71. float ratio;
  72. float teamRatio;
  73. ratio = (wins/ (float)(wins + losses));
  74. teamRatio = (team.wins/ (float)(team.wins + team.losses));
  75.  
  76. return (ratio < teamRatio);
  77. }
  78.  
  79. istream & operator>>(istream & in, ballTeam &team)
  80. {
  81. in>>team.name>>team.wins>>team.losses>>team.totalPoints;
  82.  
  83. return in;
  84. }

Template class code
  1. // Implementation of the templated version of arrayListType
  2. #include <iostream>
  3. #include <cassert>
  4. #include "arrayListTypeT.h"
  5.  
  6. using namespace std;
  7.  
  8. template<class elemType>
  9. bool arrayListTypeT<elemType>::isEmpty() const
  10. { return (length == 0); }
  11.  
  12. template<class elemType>
  13. bool arrayListTypeT<elemType>::isFull() const
  14. { return (length == maxSize); }
  15.  
  16. template<class elemType>
  17. int arrayListTypeT<elemType>::listSize() const
  18. { return length; }
  19.  
  20. template<class elemType>
  21. int arrayListTypeT<elemType>::maxListSize() const
  22. { return maxSize; }
  23.  
  24. template<class elemType>
  25. void arrayListTypeT<elemType>::print() const
  26. {
  27. for (int i = 0; i < length; i++)
  28. cout << list[i] << " ";
  29. cout << endl;
  30. }
  31.  
  32. template<class elemType>
  33. bool arrayListTypeT<elemType>::isItemAtEqual(int location, elemType item) const
  34. { return(list[location] == item); }
  35.  
  36. template<class elemType>
  37. void arrayListTypeT<elemType>::removeAt(int location)
  38. {
  39. if (location < 0 || location >= length)
  40. cout << "The location of the item to be removed "
  41. << "is out of range." << endl;
  42. else
  43. {
  44. for (int i = location; i < length - 1; i++)
  45. list[i] = list[i+1];
  46. length--;
  47. }
  48. } //end removeAt
  49.  
  50. template<class elemType>
  51. void arrayListTypeT<elemType>::retrieveAt(int location, elemType& retItem)
  52. {
  53. if (location < 0 || location >= length)
  54. cout << "The location of the item to be retrieved is "
  55. << "out of range." << endl;
  56. else
  57. retItem = list[location];
  58. } // retrieveAt
  59.  
  60. template<class elemType>
  61. void arrayListTypeT<elemType>::replaceAt(int location, elemType repItem)
  62. {
  63. if (location < 0 || location >= length)
  64. cout << "The location of the item to be replaced is "
  65. << "out of range." << endl;
  66. else
  67. list[location] = repItem;
  68.  
  69. } //end replaceAt
  70.  
  71. template<class elemType>
  72. void arrayListTypeT<elemType>::clearList()
  73. {
  74. length = 0;
  75. } // end clearList
  76.  
  77. template<class elemType>
  78. arrayListTypeT<elemType>::arrayListTypeT(int size)
  79. {
  80. if (size <= 0)
  81. {
  82. cout << "The array size must be positive. Creating "
  83. << "an array of size 100. " << endl;
  84. maxSize = 100;
  85. }
  86. else
  87. maxSize = size;
  88. length = 0;
  89. list = new elemType[maxSize];
  90. assert(list != NULL);
  91. }
  92.  
  93. template<class elemType>
  94. arrayListTypeT<elemType>::~arrayListTypeT()
  95. { delete [] list; }
  96.  
  97. template<class elemType>
  98. arrayListTypeT<elemType>::arrayListTypeT(const arrayListTypeT<elemType>& otherList)
  99. {
  100. maxSize = otherList.maxSize;
  101. length = otherList.length;
  102. list = new elemType[maxSize]; //create the array
  103. assert(list != NULL); //terminate if unable to allocate
  104.  
  105. for (int j = 0; j < length; j++) //copy otherList
  106. list [j] = otherList.list[j];
  107. }//end copy constructor
  108.  
  109. template<class elemType>
  110. int arrayListTypeT<elemType>::seqSearch(elemType item) const
  111. {
  112. int i;
  113. for (i = length - 1; i >= 0; i--)
  114. if (list[i] == item)
  115. { break; }
  116. return i;
  117. } //end seqSearch
  118.  
  119. template<class elemType>
  120. void arrayListTypeT<elemType>::insert(elemType insertItem)
  121. {
  122. // Your code to insert a new item into the list goes here.
  123. // You are required to check for the following errors:
  124. // Duplicate insertion.
  125. // Full list.
  126. // If either error occurs, display an appropriate message leave the
  127. // list unchanged.
  128.  
  129.  
  130. if(isFull())
  131. cout<<"Cannot insert item, list is full.\n"<<endl;
  132.  
  133. for(int i = 0; i < length; i++)
  134. {
  135. if(list[i] = insertItem)
  136. {
  137. cout<<"Duplicate Item in list\n"<<endl;
  138. break;
  139. }
  140. else
  141. {
  142. list[length] = insertItem;
  143. length++;
  144. }
  145. }
  146. } //end insert
  147.  
  148. template<class elemType>
  149. void arrayListTypeT<elemType>::remove(elemType removeItem)
  150. {
  151. int loc;
  152. if (length == 0)
  153. cout << "Cannot delete from an empty list." << endl;
  154. else
  155. {
  156. loc = seqSearch(removeItem);
  157.  
  158. if (loc != -1)
  159. removeAt(loc);
  160. else
  161. cout << "The team to be deleted is not in the list."
  162. << endl;
  163. }
  164. } //end remove
Last edited by power_computer; Oct 27th, 2009 at 4:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #2
Oct 27th, 2009
Well you don't seem to have written a operator>> for class ballTeam. I could be wrong since you didn't include the definition files (.h files).



I
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #3
Oct 27th, 2009
No I have written a >> operator overload, it is in the second code block at the bottom
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #4
Oct 27th, 2009
Bump...Veron where are you!?! lol XD
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 141
Reputation: power_computer is an unknown quantity at this point 
Solved Threads: 3
power_computer power_computer is offline Offline
Junior Poster
 
0
  #5
Oct 27th, 2009
Sorry for the bump, after some decent concetration I needed to overload the ostream operator the poster who replied must have had a typo, thanks anyway solved
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum


Views: 240 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC