有我的代码,但是我现在正在获取这些错误代码,我知道这里有很多错误。
因此,对于为什么某些错误会在公共类Employee中将其声明为顶部时将其不在范围内,我感到困惑。同样,当我从功率类创建子类时,为什么仍然会有错误说它不在范围内,即使它们是功率类的子类也是如此。
我正在使用const char *,但仍然出现错误。 感谢您查看我的代码!我非常感谢您的投入。
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee ();
void setEmployeeName (string name);
void setEmployeeNumber (string EmployeeID);
void setHireDate (string date);
string getEmployeeName ();
string getEmployeeNumber ();
string getHireDate ();
protected:
string employeeName;
string employeeNumber;
string hireDate;
};
Employee::Employee () {
employeeName = "Please Enter a name.";
employeeNumber = "Please enter an Employee Number";
hireDate = "Not Avaliable";
}
void Employee::setEmployeeName (string name) {
employeeName = name;
}
void Employee::setEmployeeNumber (string EmployeeID) {
employeeNumber = EmployeeID;
}
void Employee::setHireDate (string date) {
hireDate = date;
}
string Employee::getEmployeeName () {
return employeeName;
}
string Employee::getEmployeeNumber () {
return employeeNumber;
}
string Employee::getHireDate () {
return hireDate;
}
class ProductionWorker : public Employee {
public:
ProductionWorker ();
void setShift (const char * workday);
void setHourlyPayRate (double payRate);
const char * getShift ();
double getHourlyPayRate ();
void Print ();
private:
int workday;
double hourlyPayRate;
};
ProductionWorker::ProductionWorker () {
int shift = 0;
hourlyPayRate = 0.0;
}
void ProductionWorker::setShift (const char * workday) {
const char * shift = workday;
}
void ProductionWorker::setHourlyPayRate (double payRate) {
hourlyPayRate = payRate;
}
int getShift () {
if (const char * shift == 0)
return "No Shift.";
else if (const char * shift == 1)
return "Day Shift.";
else if (const char * shift == 2)
return "Night Shift.";
else
return "Error.";
}
double ProductionWorker::getHourlyPayRate () {
return hourlyPayRate;
}
void Print () {
cout << "Name of Employee: " << getemployeeName << endl
<< "Employee ID Number: " << getemployeeNumber << endl
<< "Date of Employment: " << gethireDate << endl
<< "Shift: " << getshift << endl
<< "Pay Rate: " << getHourlyPayRate << endl;
}
int main () {
ProductionWorker SystemAdmin ();
SystemAdmin.setEmployeeName ("Michael Alexander");
SystemAdmin.setEmployeeNumber ("00685193");
SystemAdmin.setHireDate ("06/20/2019");
SystemAdmin.setShift (1);
SystemAdmin.setHourlyPayRate (30.00);
SystemAdmin.Print ();
return 0;
}
这是错误代码
main.cpp: In function ‘int getShift()’:
main.cpp:79:6: error: expected primary-expression before ‘const’
if (const char * shift == 0)
^
main.cpp:79:6: error: expected ‘)’ before ‘const’
main.cpp:80:10: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
return "No Shift.";
^
main.cpp:81:11: error: expected primary-expression before ‘const’
else if (const char * shift == 1)
^
main.cpp:81:11: error: expected ‘)’ before ‘const’
main.cpp:82:10: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
return "Day Shift.";
^
main.cpp:83:11: error: expected primary-expression before ‘const’
else if (const char * shift == 2)
^
main.cpp:83:11: error: expected ‘)’ before ‘const’
main.cpp:84:10: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
return "Night Shift.";
^
main.cpp:86:10: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
return "Error.";
^
main.cpp: In function ‘void Print()’:
main.cpp:95:34: error: ‘getemployeeName’ was not declared in this scope
cout << "Name of Employee: " << getemployeeName << endl
^
main.cpp:96:33: error: ‘getemployeeNumber’ was not declared in this scope
<< "Employee ID Number: " << getemployeeNumber << endl
^
main.cpp:97:33: error: ‘gethireDate’ was not declared in this scope
<< "Date of Employment: " << gethireDate << endl
^
main.cpp:98:20: error: ‘getshift’ was not declared in this scope
<< "Shift: " << getshift << endl
^
main.cpp:99:23: error: ‘getHourlyPayRate’ was not declared in this scope
<< "Pay Rate: " << getHourlyPayRate << endl;
^
main.cpp: In function ‘int main()’:
main.cpp:106:14: error: request for member ‘setEmployeeName’ in ‘SystemAdmin’, which is of non-class type ‘ProductionWorker()’
SystemAdmin.setEmployeeName ("Longdy Chea");
^
main.cpp:107:14: error: request for member ‘setEmployeeNumber’ in ‘SystemAdmin’, which is of non-class type ‘ProductionWorker()’
SystemAdmin.setEmployeeNumber ("00685193");
^
main.cpp:108:14: error: request for member ‘setHireDate’ in ‘SystemAdmin’, which is of non-class type ‘ProductionWorker()’
SystemAdmin.setHireDate ("06/20/2019");
^
main.cpp:109:14: error: request for member ‘setShift’ in ‘SystemAdmin’, which is of non-class type ‘ProductionWorker()’
SystemAdmin.setShift (1);
^
main.cpp:110:14: error: request for member ‘setHourlyPayRate’ in ‘SystemAdmin’, which is of non-class type ‘ProductionWorker()’
SystemAdmin.setHourlyPayRate (30.00);
^
main.cpp:112:14: error: request for member ‘Print’ in ‘SystemAdmin’, which is of non-class type ‘ProductionWorker()’
SystemAdmin.Print ();
^
答案 0 :(得分:0)
有些错误很容易解决。
void ProductionWorker::Print () {
^^^^^^^^^^^^^^^^^^
和
int ProductionWorker::getShift () {
^^^^^^^^^^^^^^^^^^
Print
和getShift
是ProductionWorker
类的方法,因此必须这样定义。其他所有方法都可以做到这一点,但是这两种方法都不正确。
也不是
ProductionWorker SystemAdmin ();
但是
ProductionWorker SystemAdmin;
您所写的声明了一个名为SystemAdmin
的函数,很明显您确实需要一个名为SystemAdmin
的变量。这是一个常见的初学者错误,
不幸的是,还有其他一些难以理解的错误。您显然对轮班和工作日感到困惑。不幸的是,您的代码在这里没有多大意义,除非您可以解释您要做什么,否则我不确定该如何建议。我怀疑您是从几乎正确的代码开始的,但是在尝试对其进行修复时,它变得越来越糟,以至于不再有意义。