+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    15

    error: ISO C++ says that these are ambiguous, even though the worst conversion for th

     
    need help on this program...odd error code
    any help would be nice

    Code:
     
    employee.cpp: In function ‘std::ofstream& operator<<(std::ofstream&, const EmployeeType&)’:
    employee.cpp:138: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
    /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:183: note: candidate 1: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
    employee.cpp:136: note: candidate 2: std::ofstream& operator<<(std::ofstream&, const EmployeeType&)
    employee.cpp:141: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
    /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:361: note: candidate 1: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
    employee.cpp:136: note: candidate 2: std::ofstream& operator<<(std::ofstream&, const EmployeeType&)
    employee.cpp:142: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
    /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:628: note: candidate 1: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
    employee.cpp:136: note: candidate 2: std::ofstream& operator<<(std::ofstream&, const EmployeeType&)
    Code:
     
     
    //employee.cpp
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    using namespace std;
    #include "employee.h"
    EmployeeType::EmployeeType(int idnum, string lastName, string firstName, double salaryamt, char ins)
    {
     id = idnum;
     lastname = lastName;
     firstname = firstName;
     salary = salaryamt;
     insurance = ins;
    }
    EmployeeType::EmployeeType(const EmployeeType& rhs)
    {
     id = rhs.id;
     lastname = rhs.lastname;
     firstname = rhs.firstname;
     salary = rhs.salary;
     insurance = rhs.insurance;
    }
    /*-----------------------------------------------------------------*/
    const EmployeeType&  EmployeeType::operator = (const EmployeeType& rhs)
    {
     if(this != &rhs)
     {
      id = rhs.id;
      lastname = rhs.lastname;
      firstname = rhs.firstname;
      salary = rhs.salary;
      insurance = rhs.insurance;
     }
     return *this;
    }
    /*---------------------------------------------------------------------*/
    bool EmployeeType::operator == (const EmployeeType& rhs)
    {
     bool result = false;
     
     if(id == rhs.id &&
      lastname == rhs.lastname &&
      firstname == rhs.firstname &&
      salary == rhs.salary &&
      insurance == rhs.insurance)
       result = true;
     return result;
    }
    /*----------------------------------------------------------------------*/
    bool EmployeeType::operator != (const EmployeeType& rhs)
    {
     bool result = false;
      if(id != rhs.id ||
      lastname != rhs.lastname ||
      firstname != rhs.firstname ||
      salary != rhs.salary ||
      insurance != rhs.insurance)
       result = true;
     return result;
    }
    /*----------------------------------------------------------------------------*/
    istream& operator >> (istream& keyin, EmployeeType& rhs)
    {
     char inschoice; 
      cout << "Please enter Employee Id num." << endl;
      keyin >> rhs.id;
      cout << "Please enter last name." << endl;
      keyin >> rhs.lastname;
       cout << "Please enter first name." << endl;
      keyin >> rhs.firstname;
      cout << "Please enter salary." << endl;
      keyin >> rhs.salary;
     
      cout << "Please enter insurance options." << endl;
      cin >> inschoice;
      if (inschoice == 'n' || 'N' || 'y' || 'Y')
       { rhs.insurance = inschoice; }
      else
       { cout << "Invalid choice" << endl; }
     
     return keyin;
    }
    /*-----------------------------------------------------------*/
    ostream& operator << (ostream& keyout, const EmployeeType& rhs)
    {
     keyout << rhs.id;
     keyout << rhs.lastname;
     keyout << rhs.firstname;
     keyout << rhs.salary;
     keyout << rhs.insurance;
     return keyout;
    }
    /*-----------------------------------------------*/
    ifstream& operator >> (ifstream& filein, EmployeeType& rhs)
    {
     filein >> rhs.id;
     filein >> rhs.lastname;
     filein >> rhs.firstname;
     filein >> rhs.salary;
     filein >> rhs.insurance;
     return filein;
    }
    /*---------------------------------------*/
    ofstream& operator << (ofstream& fileout, const EmployeeType& rhs)
    {
     fileout << rhs.id;
     fileout << rhs.lastname;
     fileout << rhs.firstname;
     fileout << rhs.salary;
     fileout << rhs.insurance;
     return fileout;
    }
    /*-------------------------------------*/
    Code:
     
     
    //employee.h
    #ifndef EMPLOYEETYPE
    #define EMPLOYEETYPE
    #include <string>
    #include <iostream>
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    using std::istream;
    using std::ostream;
     
    class EmployeeType
    {
     private:
       int id;
       std::string lastname, firstname;
       double salary;
       char insurance;
     public:
      EmployeeType(int = 0, std::string = "", std::string = "", double = 0.00, char = 'n');
      EmployeeType(const EmployeeType& rhs);
    /*-----------------------------------------------------*/
      int get_id(void)
      { return id; }
      std::string get_lastname(void)
      { return lastname; }
      std::string get_firstname(void)
      { return firstname; }
      double get_salary(void)
      { return salary; }
      char get_ins(void)
      { return insurance; }
    /*-------------------------------------------------------------------*/
      void changeLname (std::string& newLname)
      { lastname = newLname; }
      void changeFname (std::string& newFname)
      { firstname = newFname; }
      void changeSalary (double& amtchange)
      { salary = salary + amtchange; }
      void changeIns (char& yes_no)
      { insurance = yes_no; }
    /*-----------------------------------------------------------------------------------*/
      const EmployeeType& operator =(const EmployeeType& rhs);
       bool operator == (const EmployeeType& rhs);
      bool operator != (const EmployeeType& rhs);
      friend istream& operator >> (istream& keyin, EmployeeType& rhs);
      friend ostream& operator << (ostream& keyout, const EmployeeType& rhs);
      friend ifstream& operator >> (ifstream& filein, EmployeeType& rhs);
      friend ofstream& operator << (ofstream& fileout, const EmployeeType& rhs);
    /*----------------------------------------------------------------------------------------------*/
    };
    #endif
    Code:
     
     
    //Program10.cpp
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    #include "employee.h"
     
     
    int main(void)
    {
     ifstream fin;
     ofstream fout;
     int choice = 0, i = 0;
     string targetname;
     EmployeeType employeearray[30], tempemp, scrap(0, "", "", 0.00, 'n');
     void menu(int& choice);
     void processchoice(int& choice, EmployeeType employeearray[], EmployeeType tempemp, int& i, ofstream& fout); 
     void closeFiles(ifstream& fin, ofstream& fout);
     void openFiles(ifstream& fin, ofstream& fout);
     int searchidnum(EmployeeType employeearray[], int& i, int& idnum);
     int searchname(EmployeeType employeearray[], int& i, string& targetname);
     void BubbleSortid(EmployeeType employeearray[], int& i);
     void BubbleSortlname(EmployeeType employeearray[], int& i);
     double salarySearchLow(EmployeeType employeearray[], int& i);
     double salarySearchHigh(EmployeeType employeearray[], int& i);
     double computeAverage(EmployeeType employeearray[], int& i);
     openFiles(fin, fout);
     while(i < 30 && fin >> employeearray[i])  //read data to array
      { ++i; }
     fout << "Program 10 Output File\nProgramers : Nathan Lesley & Brian Milum\n---------------------------\n" << endl;
     
     while (choice != 15)
     {
      menu(choice);
      processchoice(choice, employeearray, tempemp, i, fout);
     }
     
     closeFiles(fin, fout);
     return 0;
    }
    /*-----------------------------------------------------------------------------------------------*/
    void menu(int& choice)
    {
     cout << " 1 -- Add an employee to the array" << endl;
     cout << " 2 -- Remove an employee from the array" << endl;
     cout << " 3 -- Change an employee's last name" << endl;
     cout << " 4 -- Change an employee's frist name" << endl;
     cout << " 5 -- Change and employee's salary" << endl;
     cout << " 6 -- Change an employee's insurance plan" << endl;
     cout << " 7 -- Search for a particular employee based on id" << endl;
     cout << " 8 -- Search for a particular employee baded on last name" << endl;
     cout << " 9 -- Print a list of all employees in id order" << endl;
     cout << "10 -- Print a list of all employees in last name order" << endl;
     cout << "11 -- Print a list of all employees on the company insurance plan" << endl;
     cout << "12 -- Print a list of all employees not on the company insurance plan" << endl;
     cout << "13 -- Print the lowest and highest salaries of all employees" << endl;
     cout << "14 -- Compute and print the average salary of all employees" << endl;
     cout << "15 -- Quit Program" << endl;
     cout << "\n\nPlease Enter Choice : ";
     cin >> choice;
    }
    /*-----------------------------------------------------------------------------------------------*/
    void processchoice(int& choice, EmployeeType employeearray[], EmployeeType tempemp, int& i, ofstream& fout)
    { 
     int searchidnum(EmployeeType employeearray[], int& i, int& idnum);
     int searchname(EmployeeType employeearray[], int& i, string& targetname);
     void BubbleSortid(EmployeeType employeearray[], int& i);
     void BubbleSortlname(EmployeeType employeearray[], int& i);
     double salarySearchLow(EmployeeType employeearray[], int& i);
     double salarySearchHigh(EmployeeType employeearray[], int& i);
     double computeAverage(EmployeeType employeearray[], int& i); 
     int result = 0, done = 0, searchid, z = 0, f=0, passid = 0;
     string newname, searchforname;
     double newsalary, averageSal, low, high;
     char insuranceopt;
     EmployeeType scrap(0, "", "", 0.00, 'n');
    switch(choice)
    {
     case 1:  
      cin >> tempemp;
      passid = tempemp.get_id();
      result = searchidnum(employeearray, i, passid);
      if(result)
      {
       employeearray[i] = tempemp;
       ++i;
      }
      else 
       { cout << "Worker already present." << endl; }
      break;
     case 2:
      cout << "Please input idnum to search for : " << endl;
      cin >> searchid;
     
      result = searchidnum(employeearray, i, searchid);
     
      if(!result)
       { cout << "Employee not in database." << endl; }
      else
       { employeearray[result] = scrap; } 
      break;
     case 3:
      cout << "Please input id to search for :" << endl; 
      cin >> searchid;
      result = searchidnum(employeearray, i, searchid);
      if(!result)
       cout << "Employee not in database." << endl;
      else
       {   
        cout << "Please enter desired new last name : " << endl;
        cin >> newname; 
        employeearray[result].changeLname(newname); 
       }
      break;
     
     case 4:
      cout << "Please input id to search for :" << endl; 
      cin >> searchid;
      result = searchidnum(employeearray, i, searchid);
      if(!result)
       cout << "Employee not in database." << endl;
      else
       {   
        cout << "Please enter desired new first name : " << endl;
        cin >> newname; 
        employeearray[result].changeFname(newname); 
       }
      break;
     case 5:
      cout << "Please input id to search for :" << endl; 
      cin >> searchid;
      result = searchidnum(employeearray, i, searchid);
      if(!result)
       cout << "Employee not in database." << endl;
      else
       {   
        cout << "Please enter desired new salary : " << endl;
        cin >> newsalary; 
        employeearray[result].changeSalary(newsalary); 
       }
      break;
     case 6:
      cout << "Please input id to search for :" << endl; 
      cin >> searchid;
      result = searchidnum(employeearray, i, searchid);
      if(!result)
       cout << "Employee not in database." << endl;
      else
       {   
     
        while(done!=1)
        {
         cout << "Please enter desired insurance option : " << endl;
         cin >> insuranceopt;
         if(insuranceopt == 'n' ||'y')
          { employeearray[result].changeIns(insuranceopt);
            done = 1; }
         else
          { cout << "Invalid input" << endl; }
        }
     
       }
      break;
     case 7:
      cout << "Please input id to search for :" << endl; 
      cin >> searchid;
      result = searchidnum(employeearray, i, searchid);
      if(!result)
       cout << "Employee not in database." << endl;
      else
       { cout << employeearray[result] << endl; }
      break;
     case 8: 
      cout << "Please input name to search for :" << endl; 
      cin >> newname;
      result = searchname(employeearray, i, newname);
      if(!result)
       cout << "Employee not in database." << endl;
      else
       { cout << employeearray[result] << endl; }
     
      break;
     case 9:
     
      BubbleSortid(employeearray, i);
     
       for(f=0; f < i; ++f)  
      { cout << employeearray[f];
       fout << employeearray[f];
      }
      break; 
            case 10: 
      BubbleSortlname(employeearray, i);
     
      for(f=0; f < i; ++f)  
      { cout << employeearray[f];
       fout << employeearray[f];
      }
      break;
     case 11:
     
      for(z = 0; z < i; ++z)
      {
       if(employeearray[z].get_ins() == 'y' || 'Y')
        {
         cout << employeearray[z];
         fout << employeearray[z];
        }
      }
     
      break;
     case 12:
     
      for(z = 0; z < i; ++z)
      {
       if(employeearray[z].get_ins() == 'n' || 'N')
        {
         cout << employeearray[z];
         fout << employeearray[z];
        }
      }
     
      break;
     case 13:
     
      low = salarySearchLow(employeearray, i);
      high = salarySearchHigh(employeearray, i);
      cout << "Low Salary : " << low << endl;
      fout << "Low Salary : " << low << endl;
     
      cout << "High Salary : " << high << endl;
      fout << "High Salary : " << high << endl;
     
      break;
     case 14:
      averageSal = computeAverage(employeearray, i);
      cout << "Average Salary : " << averageSal << endl;
     
      break;
     case 15:
      exit(-1); break;
     } 
    }
    /*-----------------------------------------------------------------------------------------------*/
     
    void openFiles(ifstream& fin, ofstream& fout)
    {
     fin.open("program.in");
     if(!fin)
      {
       cout << "Unable to open program.in. Program will terminate" << endl;
       exit (-1);
      }
     fout.open("program.out");
     if(!fout)
      {
       cout << "Unable to open program.out. Program will terminate" << endl;
       exit (-1);
      }
    }
    /*------------------------------------------------------------------------------------------------*/
    void closeFiles(ifstream& fin, ofstream& fout)
    {
     fin.close ( );
     fout.close ( );
    }
    /*------------------------------------------------------------------------------------------------*/
    int searchidnum(EmployeeType employeearray[], int& i, int& idnum)
    {
     int found = 0, pos = 0;
     while (found != 1 && pos < i)
      {
       if(employeearray[pos].get_id() != idnum)
        { found = 0;
          ++pos; }
       else
        { found = 1; }
      }
     return pos;
    }
    /*------------------------------------------------------------------------------------------*/
     
    int searchname(EmployeeType employeearray[], int& i, string& targetname)
    {
     int found = 0, pos = 0;
     while (found != 1 && pos < i)
      {
       if(employeearray[pos].get_lastname() != targetname)
       { found = 0;
        ++pos;
       }   
       else
        found = 1;
      }
     return pos;
    }
    /*--------------------------------------------------------------------------------------*/
    void BubbleSortid(EmployeeType employeearray[], int& i)
    {  
     void swap(EmployeeType s1, EmployeeType s2);
     int x, pass=1, sorted;
       do {
          sorted = 1;
          for (x=0; x < i - pass; ++x)
          { 
      if (employeearray[i].get_id() > employeearray[i+1].get_id())
       { 
              swap(employeearray[i], employeearray[i+1]);
         sorted = 0;
       }
          }
         ++ pass;
        } while (!sorted);
    }
    /*------------------------------------------------------------------------------------*/
    void swap(EmployeeType s1, EmployeeType s2)
    {
     EmployeeType temp(0, "", "", 0.00, 'n');
     temp = s1;
     s1 = s2;
     s2 = temp;
    }
    /*---------------------------------------------------------------------------------*/
    void BubbleSortlname(EmployeeType employeearray[], int& i)
    {  
     void swap(EmployeeType s1, EmployeeType s2);
     int x, pass=1, sorted;
       do {
          sorted = 1;
          for (x=0; x < i - pass; ++x)
          { 
      if (employeearray[i].get_lastname() > employeearray[i+1].get_lastname())
       { 
              swap(employeearray[i], employeearray[i+1]);
         sorted = 0;
       }
          }
         ++ pass;
        } while (!sorted);
    }
    /*------------------------------------------------------------------------------------*/
    double salarySearchHigh(EmployeeType employeearray[], int& i)
    {
     
     double highone = 0;
     for(int z = 0; z < i; ++z)
     {
      if(employeearray[z].get_salary() > highone)
       highone = employeearray[z].get_salary();
     }
     return highone;
    }
    /*-------------------------------------------------------------------------------*/
    double salarySearchLow(EmployeeType employeearray[], int& i)
    {
     
     double lowone = 4000000;
     for(int z = 0; z < i; ++z)
     {
      if(employeearray[z].get_salary() < lowone)
       lowone = employeearray[z].get_salary();
     }
     return lowone;
    }
    /*-------------------------------------------------------------------------------*/
    double computeAverage(EmployeeType employeearray[], int& i)
    {
     double averageSal = 0, totalSal = 0;
     for(int z = 0; z < i; ++z)
     {
      totalSal += employeearray[z].get_salary();
     }
     averageSal = totalSal / i;
     return averageSal;
    }
    /*-----------------------------------------------------------------------------*/

  2. #2
    Junior Member
    Join Date
    Apr 2009
    Posts
    1
    in your employee.cpp file u need to take out the ifstream and ofstream overloads as this is overloaded when you overload the istream and ostream...should make the work compile

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    15
    oh really? couldn't you have just called me instead and told me? oh well...i'll see you monday so we can fix the bugs in the menu
    Last edited by bm159486; May 1st, 2009 at 03:07 PM.

  4. #4
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    Why don't you put all your function prototypes BEFORE main; they you only have to declare them once?

    And if you have time and want to have some fun, use a binary search on your search for employee last name.

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    15
    the other guy who commented (Fogran) and I are working on this together, and i told him we should declare those functions above main, and he said that there would be no point since only the process_choice (the one with the switch) would call them...and i hadn't thought about a binary search...

  6. #6
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    He may be thinking that they're being declared "global" and been taught that globals are a "bad" thing (and they can be). But these are just forward declarataions of functions; they aren't using any memory or anything and its just neater that way

    For classroom stuff, your linear search isn't going to be a big deal. One of my biggest shocks hitting the "real" programming world were "scope and size". Projects went from 2 week assignments to 6 to 18 month projects and file sizes went into millions of records! Speed counts when you hit that size of a file.

    Enjoy your class times...I have a A.A.B. in Computer Programming and B.S. in Computer Science...it was some of the best years of my life for sure!!!!

    Your calling a lot of functions with &i, &idnum etc. And if I remember my C++ right,this is a calling passing the address of the variable, right? But you're not changing it in the function and returning a new value? Why not pass just the value (and probably as a "const") and not the overhead of sending a memory address?
    Last edited by Rootstonian; May 3rd, 2009 at 09:07 AM.

  7. #7
    Super F@D Folder
    Join Date
    Jun 2004
    Posts
    5,091
    If you pass by value, it ends up making local copies, which can have a really high overhead. If you have large things to pass and you don't want to do it by pointer, do it by reference

    http://publib.boulder.ibm.com/infoce...ef/cplr233.htm

    yes. In c++ passing by pointer and passing by reference are different...

  8. #8
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    Well, it's been more years than I care to mention since I programmed C++ even though I try to stay in touch with it on the side from time to time.

    So you're saying what they're doing is better? Calling SetIdNumber(&id_number) vs. SetIdNumber(id_number)? I guess if that was the case, I would prototype it as SetIdNumber(const char &); (char, int, double whatever)

  9. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    15
    Thanks for all the help guys...it compiles and runs beautifully...almost. The only problem we seem to be having is when we attempt to remove an entry from the array. We can't set all the numeric values to 0 and NULL the strings because that interferes with some of the other options. What we need to do is completely erase it from the array.

    Any ideas?

  10. #10
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    Yep. Use a different data structure LOL Once you allocate memory for an array, you're stuck with it. Well, kind of <wink, wink>. Yeah, you haven't gotten into dynamic memory allocation...yet.

    For the function of your program, you just might have add a boolean value to the array: bool deleted; Then code if(!deleted)..yada,yada...

    Welcome to programming my friends! From the looks of the assignment it appears to be a last program in a Intro to Computer Science type class using C++. At least that's what I had.

    This was immediately followed by Data Structures and Algorithms I and DSA II Arrays are just the tip of the iceberg when it comes to storing data. Hope they hit you guys with Object Oriented Programming next too!! Along with linked lists, stacks, queues, binary trees et. al.

    Man, you are going to have so much fun! Damn it...I want to be back in school again

    Are you guys in college? Where at?
    Last edited by Rootstonian; May 4th, 2009 at 09:21 PM.

  11. #11
    Junior Member
    Join Date
    Apr 2009
    Posts
    15
    We actually managed to get rid of it, so thanks anyways.

    We're in Foundations of Computer Science II at Henderson State University in AR...we've already done linked lists and object oriented programming, but we've only been on C++ for about a month.

  12. #12
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    Hmmm...this is interesting now that I think about it. I haven't really coded anything like this before; I've used differnent "techniques" as far as using classes and methods.

    I wonder how the compiler handles the allocation and deallocation of the memory on this given a simpler model. Like I said, I've done it before, but I handled the memory issues to avoid any leaks.

    Given

    class Rectangle {
    public:
    // default constructor
    Rectangle::Rectangle(length=0, width=0);
    // let compiler create default destructor
    private:
    int length, width;
    };
    void main() {

    Rectangle myRectangles[500,000,000];
    // does default destructor destroy this memory allocation????
    }

  13. #13
    Caveat Emptor Rootstonian's Avatar
    Join Date
    Mar 2005
    Location
    Out of my mind
    Posts
    3,326
    Glad you got it fixed..good luck with your course work and better luck in finding a job

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Worst of Worst Case Scenario
    By cammywarrior in forum Applications and Operating Systems
    Replies: 4
    Last Post: December 30th, 2004, 11:51 PM
  2. Ambiguous Identity: What is this soundcard? Other ques. too
    By soad1789 in forum Multimedia and Audio
    Replies: 2
    Last Post: October 18th, 2004, 03:18 PM
  3. Conversion
    By flameboy1974 in forum General Tech Discussion
    Replies: 4
    Last Post: September 15th, 2002, 03:47 PM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews