no match for 'operator>>'

This is a discussion on no match for 'operator>>' within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble with the below section of code. I'm not sure what the problem is with the >>. ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    no match for 'operator>>'

    I'm having some trouble with the below section of code. I'm not sure what the problem is with the >>. Any help would be great.

    vector.cpp: In member function 'void employee::loadEmployee(std::ifstream&)':
    vector.cpp:51: error: no match for 'operator>>' in 'fin >> ((employee*)this)->employee::surname'
    vector.cpp:52: error: no match for 'operator>>' in 'fin >> ((employee*)this)->employee::hourlyRate'
    vector.cpp:53: error: no match for 'operator>>' in 'fin >> ((employee*)this)->employee::empNumber'

    Code:
          void loadEmployee(ifstream &fin) {
             fin >> surname;
             fin >> hourlyRate;
             fin >> empNumber;
          }
    Full code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <vector>
    
    #include <stdlib.h>
    
    using namespace std;
    
    class employee
    {
       /* Employee class to contain employee data
       */
       
       private:
          string surname;
          double hourlyRate;
         int empNumber;
         
       public:
          employee() {
             hourlyRate = -1;
             empNumber = -1;
             surname = "";
          }
          
          employee(const employee &other) :
             surname(other.surname),
             hourlyRate(other.hourlyRate),
             empNumber(other.empNumber)
          {
             // copy constructor
          }
          
          void setEmployee(const string &name, double rate, int num) { 
             surname = name;
             hourlyRate = rate;
             empNumber = num;
          }
          
          const string& getSurname() const {
             return surname;
          }
          
          void printEmployee() {
             cout << fixed << setprecision(2);
             cout << setw(20) << left << surname << setw(4) << empNumber << "  " << hourlyRate << "\n";
          }
          
          void loadEmployee(ifstream &fin) {
             fin >> surname;
             fin >> hourlyRate;
             fin >> empNumber;
          }
          
    };
          
    void swap(vector<employee> employees, int a, int b);
    void sortEmployees(vector<employee> &employees);
    void printEmployees(const vector<employee> &employees);
    void loadEmployees(vector<employee> &employees, const char *file);
    
    int main(int argc, char *argv[])
    {
       vector employees;
    
       if (argc != 2) {
          cout << "Syntax : employee employeefile\n";
          return 0;
       }
    
       loadEmployees(employees, argv[1]);
       printEmployees(employees);
       sortEmployees(employees);
       printEmployees(employees);
    
       return 0;
    }
    
    void loadEmployees(vector<employee> &employees, const char *file)
    {
       ifstream fin;
       employee emp;
       
       fin.open(file);
       if (!fin) {
          cout << "Unable to read from " << file << "\n";
          exit(0);
       }
       
       while (!fin.eof()) {
          emp.loadEmployee(fin);
          employees.push_back(emp);
       }
       fin.close();   
    }
    
    void printEmployees(const vector<employee> &employees)
    {
       unsigned int i;
    
       for (i=0; i<employees.size(); i++) {
          employees[i].printEmployee();
       }
       printf("\n");
    }
    
    void swap(vector<employee> employees, int a, int b)
    {
       employee temp(employees[a]);
    
       employees[a] = employees[b];
       employees[b] = temp;
    }
    
    void sortEmployees(vector<employee> &employees)
    {
       /* use selection sort to order employees, 
          in employee name order
       */
    
       int number = employees.size();
       int inner, outer, max;
    
       for (outer=number-1; outer>0; outer--)
       {
          // run though array number of times
          max = 0;
          for (inner=1; inner<=outer; inner++)
          {
             // find alphabeticaly largest surname
             if (employees[inner].getSurname() > employees[max].getSurname())
                max = inner;
          }
          if (max != outer)
          {
             // swap largest with last element in array
             swap(employees, max, outer);
          }
       }
    }

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Have you included fstream somewhere else?
    #include <fstream>

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    And I'm an idiot.

    Thanks.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    using eof to control loop - is not good - read FAQ

    you should change your loadEmployee function to be like that:

    Code:
    ifstream& loadEmployee(ifstream &fin) {
             return fin >> surname >> hourlyRate >> empNumber;
          }
    and the reading loop to be like

    Code:
    while (emp.loadEmployee(fin))
    {
          employees.push_back(emp);
    }
    otherwise - your read operation could fail - and you still add the garbage value to collection, and only after that -you were checking the state of the stream

    Also - if the stream format is incorrect you with your current code could enter the infinite loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 07:37 PM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 11:53 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. 2 array match
    By ajastru2000 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2003, 08:58 AM
  5. How do I match 2 files to print data.
    By sketchit in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-12-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21