Thread: Employee Class Program..Primary expression error help

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    3

    Employee Class Program..Primary expression error help

    So I am learning about the basics of C++ programming and I'm currently learning about classes and separate compilation in C++. However, i can't figure out why I got this error and its driving me crazy. The error that would pop up would be "38 driver.cpp expected primary-expression before '<<' token" or "39 driver.cpp expected primary-expression before '.' token". I don't quite understand what it means even after googling it.

    Heres my main
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    #include "employee.h"
    
    
    int main(){  
    Employee Emp;    
    float w, r;
    string n, l, t;
    int id;
    
    
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    
    cout << "Please enter your first and last name into the program respectively.\n";
    cin >> n >> l;
    cout << "Please enter your ID number.\n";
    cin >> id;
    cout << "Please enter 'manager' if your title is manager, if not then just enter 'employee'. \n";
    cin >>  t;
    cout << "How many hours have you worked?\n";
    cin >>  w;
    r = 10.00;
    
    
    Emp.setSalary(w, r);
    
    
    cout << "Employee's name:" << Employee.getFirstName(n) << " " << Employee.getLastName(l) << endl; 
    cout << "Employee ID Number:" << Employee.getID(id) << endl; 
    cout << "Title in company: " << Employee.getTitle(t) << << endl;
    cout << "Your salary for the week is: " << Employee.getSalary() << endl;
    
    
    system ("PAUSE");
    return 0;
    }

    my implementation file:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    #include "employee.h"
    // Implementaion of Employee Class
    //
    void Employee::setEmp(string n, string l, string t, int id, float s){
        f_name = n;
        l_name = l;
        title = t;
       this->id = id;
        salary = s;
    }
    void Employee::setSalary(float h_rate, float h_work){
         h_rate = r;
         h_work = w;
         
       if (title == "employee"){
          if (h_work > 0 && h_work < 40)
           (h_work * h_rate) = salary;
          else if (h_work > 40) { 
           overtime = h_work - 40;
           h_work = h_work - overtime;
           salary = (h_rate * h_work) + ((1.5 * h_rate) * overtime);}
           } 
       else if (title == "manager")
          if (h_work > 0 && h_work <= 40){
           (h_work * (h_rate * 2)) = salary;}
          else if (h_work > 40) { 
           overtime = h_work - 40;
           h_work = h_work - overtime;
           salary = ((2 * h_rate) * h_work) + (2.5 * h_rate) * overtime);}
           }                            
    }
    string Employee::getFirstName() {
        return f_name;
    }
    string Employee::getLastName() {
        return l_name;
    }
    string Employee::getTitle() {
        return title;
    }
    int Employee::getID(){
       return id;
    }
    float Employee::getSalary(){
      return salary;
    }
    and my header file

    Code:
    #ifndef EMPLOYEE_H
    #define EMPLOYEE_H
    #include<iostream>
    #include<string>
    using namespace std;
    /*****************************************************************
     * Version 2.0
     ******************************************************************/
    const int  W_H=40;
    class Employee {
    
    
    public:
    // the first string is for setting an emplloyye's  first name
    // the sec........
    // .....
     void setEmp(string fname, string lname, string title, int id, float s);
    //
    // thsi methid is returning an employee's first name 
     string getFirstName(); // retrieves first name of employee
     string getLastName(); // retrieves last name os employee
     void setSalary(float h_rate, float h_work);  // sets salary of employee
     string getTitle(); // retrieves title of an employee
     int getID();  // retrieves ID of an employee
     float getSalary(); // retrieves the Salary of an employee
    private:
     string f_name;
     string l_name;
     int id;
     string title;
     float s;
     float h_rate;
     float h_work;
     float overtime;
    };//
    //
    //End of the class
    //
    #endif

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Compare
    Emp.setSalary(w, r);

    With
    cout << "Employee's name:" << Employee.getFirstName(n) << " " << Employee.getLastName(l) << endl;


    You need to use an instance of the class, not the class name.


    > float w, r;
    > string n, l, t;
    Single letter variable names are all but meaningless.
    It's OK to use i,j etc for loop variables, and say x,y for coordinates.
    But pretty much everything else should be descriptive of its purpose.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    3
    The single letter variables are just to just placeholders before i edit it to look nicer, however i took your advice and now when i set all the functions to Emp."..." i received a new error saying that
    "no matching function for call to `Employee::getFirstName(std::string&)'" and "19 candidates are: std::string Employee::getFirstName() ". i dont quite understand what that means. Is it saying I am not calling the function correctly in some way?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes, the compiler is informing you that the function is being called incorrectly.

    Employee::getFirstName() is declared (within the definition of class Employee) as
    Code:
    string getFirstName();
    and (separately) defined accordingly as
    Code:
    string Employee::getFirstName() {
        return f_name;
    }
    In both of these, it does not accept any argument (other than the implied ``this'' pointer because it is a non-static member function of a class).

    If you followed Salem's advice, it is then called using something like
    Code:
         cout << Emp.getFirstName(n);
    where Emp is an instance of class Employee (which would be passed as ``this'' to the function) and n is of type string.

    So, it is being called with a single argument, when the declaration has specified the function accepts none. The compiler is complaining about an argument mismatch (passing a string to a function that has been declared as accepting no arguments). The 19 candidates means the compiler has gone through 19 functions (both within class Employee, and other function declarations) and decided none is appropriate.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    3
    Ok so i finally was able to sort throughout all the bugs and was able to finally compile XD. Now however i want to calculate the the employees salary based on my "void Employee::setSalary (float H_rate, float h_work)" function. How would i incorporate that into the "void Employee::setEmp (s, l,...)" function? Do i change setSalary from a void function and put it in the setEMP function in place of float s?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATM C++ ERROR *expected primary expression before else*
    By GrafixFairy in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2014, 10:31 AM
  2. error expected primary-expression before else eror
    By ReneeJA in forum C++ Programming
    Replies: 14
    Last Post: 02-28-2014, 01:57 PM
  3. error : expected primary-expression before ']' token
    By funnydarkvador in forum C++ Programming
    Replies: 8
    Last Post: 02-20-2013, 07:06 PM
  4. Error: expected primary expression and ; before else
    By dragonfly22 in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2012, 04:24 PM
  5. primary-expression error
    By sopa in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2012, 10:20 AM

Tags for this Thread