Thread: How to write class array to file

  1. #1
    Registered User
    Join Date
    Sep 2020

    Question How to write class array to file

    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:
    #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,
        double price = 0;
        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');
                getline(file, line, '\n');
                file >> dbline;
    //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";
      "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();
                cout << "Goodbye.\n";
    Why am I getting this error?

    I also overloaded the << operator:
    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
    file << company[i]
    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?
    Last edited by meagangramlin; 11-10-2020 at 07:40 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You declared ifstream to be an ifstream, but you're trying to use it as if it were an output stream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2020
    How silly of me I can't believe I missed that. Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class FILE to write in DOS encoding.
    By ArnoldRich in forum C Programming
    Replies: 5
    Last Post: 12-13-2013, 10:30 AM
  2. Replies: 5
    Last Post: 07-06-2011, 11:27 PM
  3. write an array of unspecified size to a file
    By c++guy in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2010, 10:54 PM
  4. Write text file to char array
    By mjh in forum C Programming
    Replies: 2
    Last Post: 04-02-2007, 08:11 AM
  5. Write to binary file from 2D array
    By wbaker01 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2006, 06:16 AM

Tags for this Thread