-
June 17th, 2009, 12:00 PM
#1
writing to a file (problem with file << )
Code:
#include <iostream>
#include <fstream> //allow writing to file instead of cout
#include <cstring>
using namespace std;
#include "date.h"
#include "person.h"
#include "student.h"
#include "Employee.h"
#include "setT.h"
void main() {
ofstream file ("test_output.txt"); //file for test results
file << "Program Output" << "\n"; //write title for test
Person *p1 = new Person("Lou", "lou@chat.ca", 20, 6, 1960);
Set<Person> s,s2,s3;
Set<int> setints;
Set<Date> dates;
s.add(*p1);
if (p1 != &(s.someElement()))
cout << "OBJECTS ARE NOT THE SAME\n"; //should not happen
s.add(*p1); //attempt to add a duplicate
s.add(*(new Person("Frank", "f123@chat.ca", 20, 3, 1967)));
s.add(*(new Person("Dave", "f23@chat.ca", 21, 12, 1961)));
s.add(*(new Student("Eric", "frank@chat.ca", Date(25, 4, 1958), "Carleton", 12345 )));
s.add(*(new Student("Mary", "mary@chat.ca", Date(25, 4, 1955), "Carleton", 22234 )));
s.add(*(new Employee("John", "johnchat.ca", Date(12, 12, 1970), "Nortel", 99912 )));
int a = 5;
int b = 12;
int c = 12;
int d = 42;
setints.add(a).add(b).add(c).add(d).add(a); //note attempt to add a duplicate
file << setints; //print the ints
//file << "Printing the Set<int> \n"; //write comment to output file
//file << setints; //print the ints to file
//print the names using the iterator.
for (s.reset(); s.hasMore(); s.advance() )
cout << s.element().getName() << " ";
//add some more people
s.add(*(new Person("Alan", "alan@chat.ca", 20, 3, 1967)));
s.add(*(new Person("Eric", "frank@chat.ca", 25, 4, 1958)));
s.add(*(new Person("Bruce", "bruce@chat.ca", 25, 4, 1955)));
//print out the three sets
//file << s << "\n" << s2 << "\n" << s3 << "\n";
//Delete all the heap objects (they should all be in set s and dates
cout << endl;
for (s.reset(); s.hasMore(); s.advance() )
{
cout << "Deleting " << (s.element()) << endl;
delete &(s.element());
}
for (dates.reset(); dates.hasMore(); dates.advance() )
{
delete &(dates.element());
cout << "test2\n";
}
file.close(); //close output file
cin.get();
} //end main
I've had to comment out anything that has to do with file << in my assignment. I get an error saying:
binary '<<' : no operator found which takes a right-hand operand of type 'Set<T>' (or there is no acceptable conversion).
Can anybody tell me what's wrong with this...here is my set class
Code:
template <class T>
class Set {
public:
Set(int initial_size = 4):index(0),numberOfElements(0)
{
capacity = initial_size;
elements = new T*[capacity];
for (int i = 0; i < capacity; i++) elements[i] = NULL;
}
Set(const Set<T> & s)
{
capacity = s.capacity;
elements = new T*[capacity];
for (int = 0; i<capacity; i++)
elements[i] = s.elements[i];
}
~Set(void){delete [] elements;} //destructor
Set<T> & operator=(const Set<T> & s)
{
if (&s != this) {
capacity = s.capacity;
elements = new T*[capacity];
for (int = 0; i<capacity; i++)
elements[i] = s.elements[i];
} return *this;
}
Set<T> & add(T & element)
{
if ((index == capacity))
{
std::cout << endl << "Container is growing." << endl << endl;
T ** temp = elements;
elements = new T*[capacity*2];
for(int i=0; i<index; i++) elements[i] = temp[i];
capacity *= 2;
delete [] temp;
}
for(int i = 0; i < capacity; i++)
if ((elements[i] != &element) && index < capacity)
{elements[index] = &element; index++; numberOfElements++; cout << element << " added" << endl; break;}
else {cout << endl << "Cannot add duplicate element." << endl << endl; break;}
return *this;
}
Set<T> & remove(const T & element)
{
if(size() < capacity/4) { //Shrinks the container when 1/4 full
std::cout << "Container is shrinking.\n";
elements = new t*[capacity/2];
for(int i=0; i<index; i++) elements[i] = temp[i];
capacity = capacity/2;
delete [] temp;
}
} // remove object equal to element if it exists
T & someElement() const
{
index = 0;
return *elements[index++];
}
//answer some (any) element of the set but don't remove it
//Iterator Methods
void reset() const {index = 0;}
bool hasMore() const {return index < numberOfElements;}
void advance() const {index++;}
T & element()const {return *elements[index];}
int size()const {return numberOfElements;} //answer the number of elements in the set.
void printOn(ostream & ostr){ostr << "hey";} //print the elements of the set
private:
T ** elements; //to store elements (like assignment #1)
int numberOfElements; //number of elements in the set
int capacity; //max. no of elements the set can current memory can hold
mutable int index; //private variable used for the iteration
};
-
June 17th, 2009, 12:57 PM
#2
Re: writing to a file (problem with file << )
Originally Posted by heat89
I get an error saying:
binary '<<' : no operator found which takes a right-hand operand of type 'Set<T>' (or there is no acceptable conversion).
Can anybody tell me what's wrong with this...
As the compiler tries to tell you. There is no operator << for Set<T>
try something like this
Code:
template <class T>
ostream & operator << ( ostream & o, const Set<T> & s ) {
s.printOn(o);
return o;
}
printOn is not const correct. It should be
Code:
void printOn(ostream & ostr) const {
ostr << "hey";
}
Kurt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|