Compiler error C2664 and C2228

This is a discussion on Compiler error C2664 and C2228 within the C++ Programming forums, part of the General Programming Boards category; Working on a basic class program and I'm generating two compiler errors that I'm not sure how to fix. Header ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    33

    Compiler error C2664 and C2228

    Working on a basic class program and I'm generating two compiler errors that I'm not sure how to fix. Header file, implementation cpp and main cpp are shown below. The specific errors are shown after the code.

    Header file
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    class Employee
    {
    public:
        Employee();
        Employee(string fName, string lName, char gen, int depend, double salary);
        double calculatePay();
        void displayEmployee();
        string getFirstName();
        void setFirstName(string fName);
        string getLastName();
        void setLastName(string lName);
        string getGender();
        void setGender(char gen);
        string getDependent();
        void setDependent(int depend);
        string getAnnualSalary();
        void setAnnualSalary(double salary);
    
    private:
        string FirstName, LastName;
        char Gender;
        int Dependent;
        double AnnualSalary;
    
    };
    Implementation file
    Code:
    #include "Employee.h";
    using namespace std;
    
    Employee::Employee()
    {
        FirstName = "not given";
        LastName = "not given";
        Gender = 'U';
        Dependent = 0;
        AnnualSalary = 20000;
    }
    
    Employee::Employee(string fName, string lName, char gen, int depend, double salary)
    {
        FirstName = fName;
        LastName = lName;
        Gender = gen;
        Dependent = depend;
        AnnualSalary = salary;
    }
    
    double Employee::calculatePay()
    {
        double WkPay;
        WkPay = AnnualSalary/52;
        return WkPay;
    }
    
    void Employee::displayEmployee()
    {
        cout << endl;
        cout << "Employee Information" << endl;
        cout << "----------------------------------" << endl;
        cout << "Employee Name: " << setw(8) << FirstName << endl;
        cout << "Gender: " << setw(8) << Gender << endl;
        cout << "Dependents: " << setw(8) << Dependent << endl;
        cout << showpoint << fixed << setprecision(2);
        cout << "Annual Salary: " << setw(8) << "$" << AnnualSalary << endl;
        cout << "Weekly Salary: " << setw(8) << "$" << calculatePay() << endl;
        cout << endl;
    }
    
    string Employee::getFirstName()
    {
        return FirstName;
    }
    
    void Employee::setFirstName(string fName)
    {
        FirstName = fName;
    }
    
    string Employee::getLastName()
    {
        return LastName;
    }
    
    void Employee::setLastName(string lName)
    {
        LastName = lName;
    }
    
    string Employee::getGender()
    {
        return Gender;
    }
    
    void Employee::setGender(char gen)
    {
        Gender = gen;
    }
    
    string Employee::getDependent()
    {
        return Dependent;
    }
    
    void Employee::setDependent(int depend)
    {
         Dependent = depend;
    }
    
    string Employee::getAnnualSalary()
    {
        return AnnualSalary;
    }
    
    void Employee::setAnnualSalary(double salary)
    { 
        AnnualSalary = salary;
    }
    The first compiler error, C2664 shows up at lines 65, 75, and 85 (same error all three lines). Error message is:

    'std::basic_string<_Elem,_Traits,_Alloc>::basic_st ring(const std::basic_string<_Elem,_Traits,_Alloc> &)' : cannot convert parameter 1 from 'char' to 'const std::basic_string<_Elem,_Traits,_Alloc> &'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>
    1> ]
    1> Reason: cannot convert from 'char' to 'const std::basic_string<_Elem,_Traits,_Alloc>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>
    1> ]
    1> No constructor could take the source type, or constructor overload resolution was ambiguous

    Main file
    Code:
    #include "Employee.h";
    using namespace std;
    
    void DisplayApplicationInformation();
    void DisplayDivider(string);
    string GetInput(string);
    void TerminateApplication();
    
    int main()
    {
        DisplayApplicationInformation();
        string input = "";
        Employee emp1;
        
        DisplayDivider("Employee #1");
        
        input = GetInput("First Name");
        emp1.setFirstName(input);
        input = GetInput("LastName");
        emp1.setLastName(input);
        input = GetInput("Gender");
        emp1.setGender(input.at(0));
        input = GetInput("Dependent");
        emp1.setDependent(atoi(input.c_str()));
        input = GetInput("Annual Salary");
        emp1.setAnnualSalary(atof(input.c_str()));
        cout << endl;
    
        emp1.displayEmployee();
    
        cout << endl;
        DisplayDivider("Employee #2");
        cout << endl;
        
        Employee emp2(string fName, string lName, char gen, int depend, double salary);
        
        emp2.setFirstName("John");
        emp2.setLastName("Worley");
        emp2.setGender("M");
        emp2.setDependent("4");
        emp2.setAnnualSalary("30000");
    
        emp2.displayEmployee();
    
        TerminateApplication();
        system("pause");
        return 0;
        
    }
    
    void DisplayApplicationInformation()
    {
        cout << "Welcome to the Employee Class Program" << endl;
        cout << "CIS247C, Wk2 Lab" << endl;
        cout << "Name: John Worley" << endl;
        cout << "The program creates an Employee class and then" << endl;
        cout << "creates two objects and displays their information." << endl;
    }
    
    void DisplayDivider(string outputTitle)
    {
        cout << "**********" << outputTitle << "**********" << endl;
    }
    
    string GetInput(string)
    {
        string input;
        cout << " Enter " << input << endl;
        getline(cin, input);
    
        return input;
    }
    
    void TerminateApplication()
    {
        cout << "Thank you for using the Employee class program" << endl;
    }
    The file generates the second error, C2228, at lines 37-41 and 43. Basically where I tried using the second created object. Error message is "left of '.setFirstName' must have class/struct/union"

    Can anyone offer any help??
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by worl4125
    The first compiler error, C2664 shows up at lines 65, 75, and 85
    Check the return types of those functions. Do they match with what you are returning?

    Quote Originally Posted by worl4125
    The file generates the second error, C2228, at lines 37-41 and 43. Basically where I tried using the second created object. Error message is "left of '.setFirstName' must have class/struct/union"
    On line 35, you wrote:
    Code:
    Employee emp2(string fName, string lName, char gen, int depend, double salary);
    This declares a function named emp2 that returns an object of type Employee. You forgot to replace the parameter declarations with the actual arguments.
    Last edited by laserlight; 11-09-2013 at 12:20 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    33
    Quote Originally Posted by laserlight View Post
    Check the return types of those functions. Do they match with what you are returning.
    Ok; I see what I was doing now. I was thinking that, in order for the input to be read in as a string that I needed to make the getter methods strings as well. I forgot that the GetInput() method was what was actually taking in the input as a string. I have the program working now.

    There is one last thing that I'm curious about. The compiler still generates a warning at line 1 on the main file (the preprocessor directive). The warning C4067:

    "unexpected tokens following preprocessor directive - expected a newline"

    Microsoft's database says that the warning generates under ANSI compatibility and has to do with the compiler finding extra following the preprocessor directive.

    Can anyone explain what this is and what fixes it?

    Thanks.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    There should be no semicolon after #include. Preprocessor directives do not use semicolons.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    33
    Quote Originally Posted by Elysia View Post
    There should be no semicolon after #include. Preprocessor directives do not use semicolons.
    Figures it was something simple like that. I'll note it for future reference. Thanks for the help.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    33

    Overloading methods - continuation of previous program

    Something that I'm a little fuzzy about here. The assignment for this week is to take the class code from last week and add overloaded methods for setDependent() and setAnnualSalary(). Both the compiler keeps giving me the "no suitable conversation" error. Lines 91 and 106 of the implementation file.

    Current file follows.
    What am I over looking here?

    Header file

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    class Employee
    {
    private:
        string FirstName, LastName;
        char Gender;
        int Dependent;
        double AnnualSalary;
        static int numEmployees;
    
    public:
        Employee();
        Employee(string fName, string lName, char gen, int depend, double salary);
        double calculatePay();
        void displayEmployee();
        string getFirstName();
        void setFirstName(string fName);
        string getLastName();
        void setLastName(string lName);
        char getGender();
        void setGender(char gen);
        int getDependent();
        void setDependent(int depend);
        void setDependent(string depend);
        double getAnnualSalary();
        void setAnnualSalary(double salary);
        void setAnnualSalary(string salary);
        static int getnumEmployees();
        static void setnumEmployees(int numEmp);
    };
    Implementation file

    Code:
    #include "Employee.h"
    using namespace std;
    
    Employee::Employee()
    {
        
        FirstName = "not given";
        LastName = "not given";
        Gender = 'U';
        Dependent = 0;
        AnnualSalary = 20000;
        numEmployees++;
        cout << "Number of employees = " << numEmployees << endl;
    }
    
    Employee::Employee(string fName, string lName, char gen, int depend, double salary)
    {
        
        FirstName = fName;
        LastName = lName;
        Gender = gen;
        Dependent = depend;
        AnnualSalary = salary;
        numEmployees++;
        cout << "Number of employees = " << numEmployees << endl;
    }
    
    double Employee::calculatePay()
    {
        double WkPay;
        WkPay = AnnualSalary/52;
        return WkPay;
    }
    
    void Employee::displayEmployee()
    {
        cout << endl;
        cout << "Employee Information" << endl;
        cout << "----------------------------------" << endl;
        cout << "Employee Name: " << setw(8) << FirstName << " " << LastName << endl;
        cout << "Gender: " << setw(12) << Gender << endl;
        cout << "Dependents: " << setw(8) << Dependent << endl;
        cout << showpoint << fixed << setprecision(2);
        cout << "Annual Salary: " << setw(5) << "$" << AnnualSalary << endl;
        cout << "Weekly Salary: " << setw(5) << "$" << calculatePay() << endl;
        cout << endl;
    }
    
    string Employee::getFirstName()
    {
        return FirstName;
    }
    
    void Employee::setFirstName(string fName)
    {
        FirstName = fName;
    }
    
    string Employee::getLastName()
    {
        return LastName;
    }
    
    void Employee::setLastName(string lName)
    {
        LastName = lName;
    }
    
    char Employee::getGender()
    {
        return Gender;
    }
    
    void Employee::setGender(char gen)
    {
        Gender = gen;
    }
    
    int Employee::getDependent()
    {
        return Dependent;
    }
    
    void Employee::setDependent(int depend)
    {
         Dependent = depend;
    }
    
    void Employee::setDependent(string depend)
    {
        Dependent = depend;
    }
    
    double Employee::getAnnualSalary()
    {
        return AnnualSalary;
    }
    
    void Employee::setAnnualSalary(double salary)
    { 
        AnnualSalary = salary;
    }
    
    void Employee::setAnnualSalary(string salary)
    {
        AnnualSalary = salary;
    }
    
    int Employee::getnumEmployees()
    {
        return numEmployees;
    }
    
    void Employee::setnumEmployees(int numEmp)
    {
        numEmployees = numEmp;
    }
    Main file

    Code:
    #include "Employee.h"
    using namespace std;
    
    void DisplayApplicationInformation();
    void DisplayDivider(string);
    string GetInput(string);
    void TerminateApplication();
    int Employee::numEmployees = 0;
    
    int main()
    {
        DisplayApplicationInformation();
        string input = "";
        
        Employee emp1;   //default constructor
        
        DisplayDivider("Employee #1");
        cout << endl;
        input = GetInput("First Name");
        emp1.setFirstName(input);
        input = GetInput("Last Name");
        emp1.setLastName(input);
        input = GetInput("Gender");
        emp1.setGender(input.at(0));    //.at() converts str to char at specific position, in this case position 0
        input = GetInput("Dependent");
        emp1.setDependent(atoi(input.c_str())); //atoi() converts str to int
        input = GetInput("Annual Salary");
        emp1.setAnnualSalary(atof(input.c_str()));  //atof() converts str to double
        cout << endl;
    
        emp1.displayEmployee();
        cout << endl;
    
        DisplayDivider("Employee #2");
        cout << endl;
        
        Employee emp2("Tom", "Smith", 'M', 4, 32500); //multi arg constructor
        
        emp2.displayEmployee();
        cout << endl;
    
    
        TerminateApplication();
        system("pause");
        return 0;
        
    }
    
    void DisplayApplicationInformation()
    {
        cout << endl;
        cout << "Welcome to the Employee Class Program" << endl;
        cout << "CIS247C, Week2 Lab" << endl;
        cout << "Developer: John Worley" << endl;
        cout << endl;
        cout << "The program creates an Employee class and then" << endl;
        cout << "calls two objects (one with default constructor" << endl;
        cout << "and one with the multi arg constructor.) It then" << endl;
        cout << "displays the information." << endl;
        cout << endl;
    }
    
    void DisplayDivider(string outputTitle)
    {
        cout << "**********" << outputTitle << "**********" << endl;
    }
    
    string GetInput(string input) //general input function; takes input as string
    {
        cout << "Enter " << input << ": ";   // no endl, in order to allow input on same line
        getline(cin, input);
        return input;
    }
    
    void TerminateApplication() // terminate program message
    {
        cout << "Thank you for using the Employee class program" << endl;
        cout << "Program terminating" << endl;
        cout << "Good-Bye" << endl;
        cout << endl;
    }
    Last edited by worl4125; 11-15-2013 at 12:48 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by worl4125
    The assignment for this week is to take the class code from last week and add overloaded methods for setDependent() and setAnnualSalary(). Both the compiler keeps giving me the "no suitable conversation" error. Lines 91 and 106 of the implementation file.
    The error message is probably saying something about "no suitable conversion". So, start looking at what gets converted to what:
    Code:
    void Employee::setDependent(string depend)
    {
        Dependent = depend;
    }
    Okay, we see that Dependent is a member variable, an int. depend is a parameter, a std::string. Therefore, you are trying to assign a std::string to an int, but there is no suitable conversion from std::string to int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    33
    Quote Originally Posted by laserlight View Post
    The error message is probably saying something about "no suitable conversion". So, start looking at what gets converted to what:
    Code:
    void Employee::setDependent(string depend)
    {
        Dependent = depend;
    }
    Okay, we see that Dependent is a member variable, an int. depend is a parameter, a std::string. Therefore, you are trying to assign a std::string to an int, but there is no suitable conversion from std::string to int.
    Yeah, that part I get. I guess what I'm confused with is what the professor wants. The original code does the conversions in the main file. The main() uses a method called GetInput() to accept the user input as a string and then uses atoi() or atof() to convert from string to int or double, depending on the attribute that is being input at the time.

    What the professor is asking for is overloaded methods for both setDependent() and setAnnualSalary() with string as the parameter, instead of their original data type. I would then need to convert the input back to the original data type, int or double, as needed.

    I'm at a lost as to what to set the new methods to. Do I need to set up the atoi() and atof() conversions inside the new set() methods?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by worl4125
    What the professor is asking for is overloaded methods for both setDependent() and setAnnualSalary() with string as the parameter, instead of their original data type. I would then need to convert the input back to the original data type, int or double, as needed.

    I'm at a lost as to what to set the new methods to. Do I need to set up the atoi() and atof() conversions inside the new set() methods?
    Ah. Yes, except that you should not use atoi or atof because they don't do error checking (or rather, the result of an error is indistinguishable from valid input). You should either use a stringstream or something in the strtod family of functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PLEASE HELP!! error C2228
    By 5.5.7. Will in forum C++ Programming
    Replies: 35
    Last Post: 03-27-2013, 09:40 PM
  2. Error C2109 + C2228
    By deeisenberg in forum C Programming
    Replies: 8
    Last Post: 04-23-2012, 11:04 AM
  3. Compiler error c2228
    By <showMe> in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2006, 08:31 PM
  4. Need Help on C2664 error
    By jamez05 in forum C++ Programming
    Replies: 8
    Last Post: 10-28-2005, 12:40 PM
  5. Error C2664
    By carisma36 in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2004, 02:22 AM

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