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.
// main.cpp
//Calculates and displays a new salary
#include <iostream>
#include <string>
#include <iomanip>
#include "Employee.h"
#include <cmath>
usingnamespace 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*).
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.