0

In my OpenFile function it should prompt user to enter a file name and read the file into an array. I keep getting the error: no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream<char>}' and 'entryType'). I have done some research and found some similar questions related to this error. I didn't find the questions that helpful because they were poorly written. I think the problem could be using a void function or declaring the array as part of entryType. I know that I'm getting this error because the compiler looked for a function that could handle (istream) >> (entryType) but found none. How would I fix my code to get rid of this error?

Header File

include<string>
using namespace std;

enum Title {Mr, Mrs, Ms, Dr, NA};

struct NameType {
  Title title;
  string firstName;
  string lastName;
};

  struct AddressType {
  string street;
  string city;
  string state;
  string zip;
 };

struct PhoneType {
  int areaCode;
  int prefix;
  int number;
};

struct entryType {
  NameType name;
  AddressType address;
  PhoneType phone;
};

const int MAX_RECORDS = 50;

Code

entryType bookArray[MAX_RECORDS]; // entryType declared in header file

int main()
{
   entryType userRecord;
   string filename;
   ifstream inData;
   char searchOption;

   OpenFile(filename, inData);

   MainMenu(inData, filename);

   return 0;
}

void OpenFile(string& filename, ifstream& inData)
{
   do {
       cout << "Enter file name to open: ";
       cin >> filename;

       inData.open(filename.c_str());

       if (!inData)
           cout << "File not found!" << endl;

   } while (!inData);


   if(inData.is_open())
   {

       for(int i=0; i<MAX_RECORDS;i++)
       {
           inData >> bookArray[i];

       }
   }
}
user38099
  • 23
  • 5
  • 4
    There is no `operator>>` for the type `entryType`. `std::ifstream` simply doesn't know how to read from a file to a `entryType`. You need to overload `operator>>` for your type. See [this question](http://stackoverflow.com/questions/4421706/operator-overloading) on what operator overloading is and how to do it. – François Andrieux Apr 13 '17 at 18:42
  • @FrançoisAndrieux: I'm a little jealous of OP. To assume you'd get some nice behavior for file I/O by default for objects. In C++. – AndyG Apr 13 '17 at 18:44
  • 1
    Where did you tell the computer how to interpret a sequence of character as an `entryType`? And in what way did your C++ book not explain this? – Lightness Races in Orbit Apr 13 '17 at 19:23
  • I was able to fix my code without operator overloading. Thanks for all of the suggestions. – user38099 Apr 13 '17 at 20:20

2 Answers2

0

You just should overload operator>>.

std::istream& operator>>(std::istream& is, T& object)
{
  // Read object from stream
  return is;
}

where T will be your created types.

Davit Tevanian
  • 861
  • 8
  • 16
0
void OpenFile(string& filename, ifstream& inData)
{
   do {
      cout << "Enter file name to open: ";
      cin >> filename;

      inData.open(filename.c_str());

      if (!inData)
          cout << "File not found!" << endl;

    } while (!inData);


if(inData.is_open())
{

    for(int i=0; i<MAX_RECORDS;i++)
    {
        inData >> bookArray[i].name.firstName;

    }
  }
}
user38099
  • 23
  • 5