| | |
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:
Solved Threads: 3
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
Class I am passing to template as parameter
Template class code
Error line 28 : templateclass.h std::ostream<<ballteam is illegal
driver.cpp line 30 Where instationating templateclass<ballTeam>::print const ()
driver
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include "arrayListTypeT.h" #include "ballTeam.h" using namespace std; int main() { arrayListTypeT<ballTeam> teamList(10); ballTeam team; fstream fin; fin.open("teamStats.data"); if(!fin.is_open()) cout<<"Error: File does not exist or error opening file\n"<<endl; while(!fin.eof()) { fin>>team; teamList.insert(team); } fin.close(); teamList.print(); return 0; }
Class I am passing to template as parameter
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "ballTeam.h" using namespace std; ballTeam::ballTeam() { name = ""; wins = 0; losses = 0; totalPoints = 0; } ballTeam::ballTeam(string tName, int tWins, int tLosses, int tTotal) { name = tName; wins = tWins; losses = tLosses; totalPoints = tTotal; } void ballTeam::set(string tName, int tWins, int tLosses, int tTotal) { name = tName; wins = tWins; losses = tLosses; totalPoints = tTotal; } string ballTeam::getName() const { return name; } int ballTeam::getWins() const { return wins; } int ballTeam::getLosses() const { return losses; } int ballTeam::getTotal() const { return totalPoints; } bool ballTeam::operator==(const ballTeam &team) { float ratio; float teamRatio; ratio = (wins/ (float)(wins + losses)); teamRatio = (team.wins/ (float)(team.wins + team.losses)); return (ratio == teamRatio); } bool ballTeam::operator>(const ballTeam &team) { float ratio; float teamRatio; ratio = (wins/ (float)(wins + losses)); teamRatio = (team.wins/ (float)(team.wins + team.losses)); return(ratio > teamRatio); } bool ballTeam::operator<(const ballTeam &team) { float ratio; float teamRatio; ratio = (wins/ (float)(wins + losses)); teamRatio = (team.wins/ (float)(team.wins + team.losses)); return (ratio < teamRatio); } istream & operator>>(istream & in, ballTeam &team) { in>>team.name>>team.wins>>team.losses>>team.totalPoints; return in; }
Template class code
C++ Syntax (Toggle Plain Text)
// Implementation of the templated version of arrayListType #include <iostream> #include <cassert> #include "arrayListTypeT.h" using namespace std; template<class elemType> bool arrayListTypeT<elemType>::isEmpty() const { return (length == 0); } template<class elemType> bool arrayListTypeT<elemType>::isFull() const { return (length == maxSize); } template<class elemType> int arrayListTypeT<elemType>::listSize() const { return length; } template<class elemType> int arrayListTypeT<elemType>::maxListSize() const { return maxSize; } template<class elemType> void arrayListTypeT<elemType>::print() const { for (int i = 0; i < length; i++) cout << list[i] << " "; cout << endl; } template<class elemType> bool arrayListTypeT<elemType>::isItemAtEqual(int location, elemType item) const { return(list[location] == item); } template<class elemType> void arrayListTypeT<elemType>::removeAt(int location) { if (location < 0 || location >= length) cout << "The location of the item to be removed " << "is out of range." << endl; else { for (int i = location; i < length - 1; i++) list[i] = list[i+1]; length--; } } //end removeAt template<class elemType> void arrayListTypeT<elemType>::retrieveAt(int location, elemType& retItem) { if (location < 0 || location >= length) cout << "The location of the item to be retrieved is " << "out of range." << endl; else retItem = list[location]; } // retrieveAt template<class elemType> void arrayListTypeT<elemType>::replaceAt(int location, elemType repItem) { if (location < 0 || location >= length) cout << "The location of the item to be replaced is " << "out of range." << endl; else list[location] = repItem; } //end replaceAt template<class elemType> void arrayListTypeT<elemType>::clearList() { length = 0; } // end clearList template<class elemType> arrayListTypeT<elemType>::arrayListTypeT(int size) { if (size <= 0) { cout << "The array size must be positive. Creating " << "an array of size 100. " << endl; maxSize = 100; } else maxSize = size; length = 0; list = new elemType[maxSize]; assert(list != NULL); } template<class elemType> arrayListTypeT<elemType>::~arrayListTypeT() { delete [] list; } template<class elemType> arrayListTypeT<elemType>::arrayListTypeT(const arrayListTypeT<elemType>& otherList) { maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; //create the array assert(list != NULL); //terminate if unable to allocate for (int j = 0; j < length; j++) //copy otherList list [j] = otherList.list[j]; }//end copy constructor template<class elemType> int arrayListTypeT<elemType>::seqSearch(elemType item) const { int i; for (i = length - 1; i >= 0; i--) if (list[i] == item) { break; } return i; } //end seqSearch template<class elemType> void arrayListTypeT<elemType>::insert(elemType insertItem) { // Your code to insert a new item into the list goes here. // You are required to check for the following errors: // Duplicate insertion. // Full list. // If either error occurs, display an appropriate message leave the // list unchanged. if(isFull()) cout<<"Cannot insert item, list is full.\n"<<endl; for(int i = 0; i < length; i++) { if(list[i] = insertItem) { cout<<"Duplicate Item in list\n"<<endl; break; } else { list[length] = insertItem; length++; } } } //end insert template<class elemType> void arrayListTypeT<elemType>::remove(elemType removeItem) { int loc; if (length == 0) cout << "Cannot delete from an empty list." << endl; else { loc = seqSearch(removeItem); if (loc != -1) removeAt(loc); else cout << "The team to be deleted is not in the list." << endl; } } //end remove
Last edited by power_computer; Oct 27th, 2009 at 4:32 pm.
![]() |
Similar Threads
- Strange Word restart error? (Viruses, Spyware and other Nasties)
- template compile error (C++)
- Template problem - error status -1073741819 (C++)
- Error submitting message (DaniWeb Community Feedback)
- VERY strange device manager error!! (Windows NT / 2000 / XP)
- code error (ColdFusion)
- Strange system booting error (Windows NT / 2000 / XP)
- Parse error: (PHP)
Other Threads in the C++ Forum
- Previous Thread: Problem regarding "Fatal Error LNK1561"
- Next Thread: precision issues when printing to cout
Views: 240 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





