Classes and Objects - Getting two errors when compiling program

I am a distance student taking C++ for the first time (not by choice, it is a requirement for my BSc program) and have briefly touched on Classes and Objects. I will be honest, I am still unsure of them. I have been given an assignment where I have to finish coding (some of the coding has already been provided) a program that will calculate an display a new salary based on what the end user enters. I have written and rewritten this so many times that I managed to get it from 78 errors down to two when compiling. Can someone please look at it and let me know where it is I could be going wrong? It is currently 3:49AM AST and there is no one else that I can ask. Thanks for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <string>

using std::string;

// declaration section
class Employee
{
  public:
    Employee(string, double); 
    string getName();  
    double getPay();  
    void setName(string); 
    void setPay(double); 
    void calcNewSalary(double); 
  private:
    string name;
    double pay;
};

// implementation section
Employee::Employee(string Name, double Pay): name(Name), pay(Pay)  {

}

string Employee::getName() {
  return name;
}

void Employee::setName(string Name) {
  name = Name;
}

double Employee::getPay() {
  return pay;
}

void Employee::setPay(double Pay) {
  pay = Pay;
}

void Employee::calcNewSalary(double raise) {
  pay += pay*raise;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

//  main.cpp
//Calculates and displays a new salary
#include <iostream>
#include <string>
#include <iomanip>
#include "Employee.h"
#include <cmath>

using namespace std;


int main()
{    
    //create Employee object
    Employee employeeRaise;

    //declare variables
    string name = "";
    double pay  = 0;
    double rate = 0.0;
    double raise=0.0;

    //get name, salary, and raise percentage
    cout << "Employee's name: ";
    getline(cin, name);
    cout << "Employee's current salary: ";
    cin >> pay;
    cin.ignore(100, '\n');
    cout << "Raise rate: ";
    cin >> rate;
    cin.ignore(100, '\n');
    

    //assign name and salary to the Employee object
    employeeRaise = new Employee(name, pay);

    //use the Employee object to display the name and current salary
    cout<< "Employee : " << employeeRaise.getName() << endl;
	cout<< "Employee Salary : " << employeeRaise.getPay() << endl;

    //use the Employee object to calculate the new salary
    employeeRaise.calcNewSalary(rate);


    //use the Employee object to display the new salary    
    cout<< "Employee new Salary : " << employeeRaise.getPay() << endl;

	return 0;
}  // end of main function



I am used Microsoft VS 2005 and it is returning the following errors:



\cpp5\chap14\ch14cone01 solution\ch14cone01 project\ch14cone01.cpp(17) : error C2512: 'Employee' : no appropriate default constructor available

cpp5\chap14\ch14cone01 solution\ch14cone01 project\ch14cone01.cpp(37) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Employee *' (or there is no acceptable conversion)

\cpp5\chap14\ch14cone01 solution\ch14cone01 project\employee.h(18): could be 'Employee &Employee::operator =(const Employee &)'while trying to match the argument list '(Employee, Employee *)'
On line 16, you can't do that because you don't have a default constructor defined. Defining any constructor will remove the default constructor the compiler generates.

Line 36, you can't use new with a normal object. new is for creating pointers (for your object, employee*).
Ok...

For line 36m I removed the new . That is no longer reporting errors.

As for line 16, I defined the constructor as Employee::Employee(string, double); and it is still returning the error relating to no appropriate default constructor available.

I am not sure at this point what I should be entering in as the constructor.
closed account (z05DSL3A)
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.

So you could add Employee(); or change Employee(string, double) to somthing like: Employee(string = "", double =0);
Last edited on
Thank you!!! The Employee(string = "", double =0); is what fixed it.

Not only that, I've learned a valuable lesson regarding constructors.

Topic archived. No new replies allowed.