0

I am currently working on a program for class that requires me to overload the stream extraction operator, >>, to take data from a file straight into a class. I am getting a:

error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion)

Here is the specific code the error is affecting.

int main()

#include <iostream>
#include <fstream>
#include <iomanip>
#include "stockType.h"
#include "stockListType.h"

using namespace std;

stockType myStock;
stockListType stockList;
ifstream infile;
infile.open("StockData.txt");

infile >> myStock;

stockType.h Header File

#ifndef STOCKTYPE_H
#define STOCKTYPE_H

#include <string>
#include <fstream>
#include <iostream>


class stockType
{
public:
    stockType();
    void printStock();
    void calcPercent();



    char Symbol[3];
    float openingPrice;
    float closingPrice;
    float todayHigh;
    float todayLow;
    float prevClose;
    int volume;
    float percent;

    friend std::ifstream &operator >> (std::ifstream &in, const stockType &myStock);
};

#endif

stockType.cpp Resource File

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "stockType.h"

std::ifstream& operator>> (std::ifstream &in, const stockType &myStock)
{
in >> myStock.Symbol;
in >> myStock.openingPrice;
in >> myStock.closingPrice;
in >> myStock.todayHigh;
in >> myStock.todayLow;
in >> myStock.prevClose;
in >> myStock.volume;

return in;
}

most of the searching I have done is people having problems using ostream to do this and are getting their data during program use. Trying to erorr-correct using the ifstream and reading straight from a txt file has been difficult. I can provide whatever additional information is needed. Any help is much appreciated. thanks.

  • 1
    Think twice!! **`const`** `stockType &myStock` surely won't work to provide **writing** anything to this parameter :P ... – πάντα ῥεῖ Aug 04 '14 at 22:18
  • Further reading: http://stackoverflow.com/questions/4421706/operator-overloading – Deduplicator Aug 04 '14 at 22:47
  • @πάντα ῥεῖ Great point. For some reason it eluded me that const wouldn't allow me to write to it, which would take away the point of const in the first place! – TimeStamp12 Aug 05 '14 at 03:42

1 Answers1

3

Your input operator signature

std::ifstream& operator>> (std::ifstream &in, const stockType &myStock);
                                           // ^^^^^

makes no sense. To input anything from the stream to the myStock parameter, it has to be non const of course. Also you usually don't want to overload specific implementations of std::istream, thus your signature should look like this:

std::istream& operator>> (std::istream &in, stockType &myStock);
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • In general shouldn't it be std::istream& instead of std::ifstream&? I don't know if the assignment was to do it for ifstream or not. – medgno Aug 04 '14 at 22:36
  • Yeah. the assignment for this particular part states: For example, suppose infile is an ifstream object and the input file was opened using the object infile. Further suppose that myStock is a stock object. Then the statement: infile >> myStock; reads the data from the input file and stores it in the object myStock. (Note that this statement reads and stores the data in the relevant component of myStock) – TimeStamp12 Aug 04 '14 at 22:40
  • @user3220776 Yeah! And why don't you do it? Is there something you don't understand from my answer? I'm willing to explain more if necessary. – πάντα ῥεῖ Aug 04 '14 at 22:45
  • Oh, makes sense. I had actually seen it suggested with the const when looking up the overloading concept; having been completely unfamiliar with overloading infile straight to a class. Thanks – TimeStamp12 Aug 04 '14 at 23:05
  • @TimeStamp12 You might have seen it with the explanations for the output `std::ostream& operator<<(std::ostream&, const T&)` concepts. For output the `const` perfectly makes sense (the reference parameter won't be changed). – πάντα ῥεῖ Aug 04 '14 at 23:08
  • @πάντα ῥεῖ I looked up the original place I took it from. The poster was trying to use an unreferenced "State" class variable (akin to myStock) when passing to the function:: `ifstream& operator >> (ifstream& in, State h)` .. so the person helping said he needs to pass the State by reference, including the const in his answer:: `ifstream& operator >> (ifstream& in, const State &h)` .. So i had changed State to myStock. looking back I hadn't noticed the poster mention he was still having some problems with it – TimeStamp12 Aug 05 '14 at 03:38