Hello. I am trying to write the content of a class array to a file but I keep getting this error:
no operator "<<" mathces these operands.
This is where im getting the error:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "Node.h"
#include "Stock.h"
#include "BinarySearchTree.h"
int main()
{
BinarySearchTree<Stock> tree;
Stock company[23];
ifstream file;
string name,
symbol;
double price = 0;
file.open("data.txt");
if (!file)
cout << "Can not open file.\n";
//read data from file and store into class object, then store into binary search tree
while (!file.eof())
{
for (int i = 0; i < 20; i++)
{
getline(file, line, '\n');
company[i].setName(line);
getline(file, line, '\n');
company[i].setSymbol(line);
file >> dbline;
company[i].setPrice(dbline);
file.get();
}
}
file.close();
//other code blah doesn't matter
//now this is where i get the error
else if (choice == QUIT)
{
cout << endl;
cout << "Now write data to file...\n";
file.open("data.txt", ios::out);
for (int i = 0; i < 23; i++)
{
file << company[i].getName(); //i get the error one these 3 lines
file << company[i].getSymbol();
file << company[i].getPrice();
}
file.close();
cout << "Goodbye.\n";
}
}
Why am I getting this error?
I also overloaded the << operator:
Code:
ostream &operator<<(ostream& os, const Stock& s)
{
os << s.compName << endl << s.compSymbol << endl << s.compPrice << endl;
return os;
}
In main(), in the loop to write data to the file, i tried just writing but i still got the same error message.
I also have another question just out of curiosity. If I wanted to store the contents of the binary search tree instead of the class array into the file, how could I go about doing that instead?