Error when using infile in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deenar
    New Member
    • Sep 2010
    • 13

    Error when using infile in C++

    Hi i'm trying to open a file and then pass it onto another class file. Below is the main.cpp, the error msg i am receiving is for the line: infile >> D;

    Code:
    Main. cpp
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "DaysData.h"
    #include "VectorFile.h"
    
    
    using namespace std;
    
    int main()
    {
        ifstream infile;
        ofstream outFile;
        infile.open( "fileListAug.txt");             // Retrieves the file names for a the month from a particular txt.
    
        if( !infile )
        {
            cout << "Cannot open input file"<< endl;  // If file cannot open display an error message.
            return -1;
        }
    
        DaysData D;
        infile >> D;  [B]//error line[/B]
    
        return 0;
    }
    error:
    error: no match for 'operator>>' in 'infile >> D

    If someone could please help me get rid of this error msg.

    thanks
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The operators << and >> on input and output streams are only over ridden for the basic types plus std::string. If you want to get data for your class DaysData then you either need to get the individual bits of data as basic types and construct a DaysData class from them or you need to overload operator>> for DaysData, you would need the following signiture

    Code:
    // This might need to be a friend of DaysData so it can access the internal private data
    istream& operator>> (istream& is, DaysData& dd )
    {
      // code to extract DaysData from is
    
      return is;
    }
    You can just call infile >> D; because the compiler/library has no idea how to extract a DaysData from a stream.

    Comment

    Working...