My first class, errors, help

This is a discussion on My first class, errors, help within the C++ Programming forums, part of the General Programming Boards category; This is my first attempt at creating a class. I am a little frustrated because I still have errors that ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    My first class, errors, help

    This is my first attempt at creating a class. I am a little frustrated because I still have errors that I don't understand. What am I doing wrong? Help!


    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    using std::cout;
    using std::endl;
    
    //Class to calculate pay for an employee
    class Employee
    {
    // data declaration section
    private:
                   int _empId;
                   double _payRate;
                   double _hoursWorked;
    
    //methods declaration section
    public:
    	Employee();
    	void showEmployeeValues();
    	void setNewEmployeeValues();
    	void calculatePayValues();
    };
    
    // methods implementation section
    Employee::Employee()
    {
    	_empId = 1;
    	_payRate = 5.33;
    	_hoursWorked = 10.0;
    
    	cout << "Employee default constructor created. \n\n";
    }
    
    // Calculate pay for an employee
    
    void Employee::showEmployeeValues()
    {
    
    cout<< " _empId = " << empId;
    	<< "\n _payRate = << payRate;
    	<< "\n _hoursWorked = << hoursWorked<< endl;
    }
    
    void Employee::calculatePayValues()
    
    {
    	cout << empId * payRate * hoursWorked;
    }
    
    int main()
    {
     Employee payOne;
    
     cout << "The value for this pay period is:  \b";
     payOne.showEmployeevaules();
     cout << "\nThe pay amount is:   ";
    
    	return 0;
    }


    The errors are:
    error C2065: 'empId' : undeclared identifier
    error C2593: 'operator <<' is ambiguous
    error C2001: newline in constant
    error C2143: syntax error : missing ';' before '<<'
    error C2065: 'hoursWorked' : undeclared identifier
    error C3861: 'empId': identifier not found, even with argument-
    dependent lookup
    error C2039: 'showEmployeevaules' : is not a member
    of 'Employee'

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Basically you forgot some _ like in
    _empId <> empId and _payRate <> payRate.
    then there are some missing " and you missspelled showEmployeevaules <> showEmployeeValues
    Kurt

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,937
    Well, your member variable is named _empId, but on occasion you write empId.

    This is particularly problematic:
    Code:
    cout<< " _empId = " << empId; // <- you have a terminating semi-colon!
    	<< "\n _payRate = << payRate; // your string literal is not properly delimited!
    	<< "\n _hoursWorked = << hoursWorked<< endl; // same string literal problem!
    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

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You had showEmployeevaules(); when it should be showEmployeeValues(); There are also some other small errors.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    i know this isnt going to help fix it but you do not need to put
    using std::cout;
    using std::endl;


    when u have <iostream> and using namespace std; in the code.

    Hugo.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    Revised

    I finally got this thing to compile but it does not display the calculations. How do i modify it so that the output actually shows what the pay is?

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    
    //Class to calculate pay for an employee
    class Employee
    {
    // data declaration section
    private:
    	int _empId;
        double _payRate;
    	double _hoursWorked;
    
    //methods declaration section
    public:
    	Employee();
    	void showEmployeeValues();
    	void setNewEmployeeValues();
    	void calculatePayValues();
    };
    
    // methods implementation section
    Employee::Employee()
    {
    	_empId = 1;
    	_payRate = 5.33;
    	_hoursWorked = 10.0;
    
    	cout << "Employee default constructor created. \n\n";
    }
    
    // Calculate pay for an employee
    
    void Employee::showEmployeeValues()
    {
    
    cout << " _empId = " << _empId << endl;
    cout<< "\n _payRate = "<< _payRate<< endl;
    cout<< "\n _hoursWorked = "<< _hoursWorked<< endl;
    }
    
    void Employee::calculatePayValues()
    
    {
    	cout << " empId * payRate * hoursWorked";
    }
    
    int main()
    {
     Employee payOne;
    
     cout << "The value for this pay period is:  \n";
     payOne.calculatePayValues();
    
     cout << "\nThe pay amount is:   ";
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    20

    Lightbulb

    Code:
    int Employee::calculatePayValues()
    
    {
    	cout << " empId * payRate * hoursWorked";
    	return( payRate * _hoursWorked);
    //Presuming that you dont want to multiply _empId 
    //which is what you have cout-ed
    }
    
    int main()
    {
     Employee payOne;
    
     cout << "The value for this pay period is:  \n";
     payOne.calculatePayValues();
    
    cout << "\nThe pay amount is:   "<<payOne.calculatePayValues();
    //cout-ing the return value of employee
     
    	return 0;
    }
    btw you will have to declare Employee as either float or int

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    Thanks everyone. I'll try all that was suggested

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  2. Class Errors (LNK2005)
    By NMZ in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2005, 02:52 PM
  3. errors in my class....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 03:22 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM

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