C ++无法从方法中计算工资
C++ unable cout salary from method
入口点是 int main() 所以我尝试调用 pwr.GetSalary 来计算外部字符串“Salary”和双精度值,但是程序没有打印出任何内容。
所以它是基数 class.
class Employee
{
public:
std::string FirstName;
std::string LastName;
std::string Patronymic;
double Salary;
Employee() {};
explicit Employee(std::string FirstName, std::string LastName,
std::string Patronymic, double Salary)
: FirstName(FirstName), LastName(LastName),
Patronymic(Patronymic), Salary(Salary) {}
bool operator==(Employee other) const
{
if (this->FirstName == other.FirstName &&
this->LastName == other.LastName &&
this->Patronymic == other.Patronymic)
return true;
else
return false;
}
};
一个女儿 class 继承了基数 class...这是计算工资并打印出来的绝妙方法...
class Papersworker : public Employee
{
private:
std::string FirstName;
std::string LastName;
std::string Patronymic;
double Salary;
public:
Papersworker() {};
using Employee::Employee;
const std::string Job = "Papersworker";
std::map<std::string, double> Coefficient =
{
{"Director", 4.2},
{"Papersworker", 1.2},
{"Guardian", 1.5},
{"Programmer", 2.5}
};
void ChangeCoefficient(std::string Job, double NewCoefficient)
{
Coefficient[Job] = NewCoefficient;
}
void ChangeNameSalary(std::string FirstName, std::string LastName, std::string Patronymic, double Salary)
{
this->FirstName = FirstName;
this->LastName = LastName;
this->Patronymic = Patronymic;
this->Salary = Salary;
}
void PrintPapersworker()
{
std::cout << "First name\t" << "Lastname\t" << "Patronymic\t" << "Salary\n" << this->FirstName << "\t\t" << this->LastName << "\t" << this->Patronymic << "\t" << this->Salary << "\n" << std::flush;
for (const auto& i : this->Coefficient)
{
std::cout << i.first << " = " << i.second << ";\t" << std::flush;
}
std::cout << "\n------------------------------------------------------------\n" << std::flush;
}
double GetSalary(double Salary, std::string Job)
{
return Salary * this->Coefficient[Job];
}
};
精彩的 int main() 部分。
int main()
{
Papersworker pwr;
double sr = 0.0;
std::cout << "\nEnter director's salary\t" << std::flush; std::cin >> sr;
std::cout << "\nSalary\t" << pwr.GetSalary(sr, "Director");
return 0;
}
如果您看到一些不好的需要优化的地方,请不要介意回复。 ._.我不明白 classes 建筑技巧是怎么回事。 https://pastebin.com/p7HXaX80
P. S. 我的作业强制使用私人名字,姓氏,父名,薪水......
P. S. S. 但是,我现在使用 Visual Studio 2022 Preview 和最新的 C++。
https://imgur.com/a/N8cDK3n
Papersworker
的所有私有成员都隐藏了 Employee
的 public 成员
class Papersworker : public Employee {
private:
std::string FirstName;
std::string LastName;
std::string Patronymic;
double Salary;
// ...
};
试试这个:Compiler Explorer(未测试,因为我现在很着急,回来时会看看)
另外,be careful 和 std::map::operator []
您的代码将无法编译,因为 sr
变量未定义。
在 main() 的语句 std::cin >> sr;
中使用它之前定义 double sr;
并且(至少)程序将编译并与用户交互。
int main() {
Papersworker pwr;
std::cout << "\nEnter director's salary\t" << std::flush;
double sr; // <- your missed variable
std::cin >> sr;
std::cout << "\nSalary\t" << pwr.GetSalary(sr, "Director");
}
程序使用输入 10 打印:
Enter director's salary 10
Salary 42
program does not print out anything
如果您将 _getch()
更改为 getch()
,您的程序(您提供的 pastebin link)编译成功并连续打印,可以看到 here。但出于某种原因,它永远持续下去。由于您提供的 link 有大约 500 行代码,所以我没有查看为什么条件没有被破坏(或者程序是否有任何未定义的行为)。也许您可以缩小问题范围并再次编辑您的问题。
入口点是 int main() 所以我尝试调用 pwr.GetSalary 来计算外部字符串“Salary”和双精度值,但是程序没有打印出任何内容。 所以它是基数 class.
class Employee
{
public:
std::string FirstName;
std::string LastName;
std::string Patronymic;
double Salary;
Employee() {};
explicit Employee(std::string FirstName, std::string LastName,
std::string Patronymic, double Salary)
: FirstName(FirstName), LastName(LastName),
Patronymic(Patronymic), Salary(Salary) {}
bool operator==(Employee other) const
{
if (this->FirstName == other.FirstName &&
this->LastName == other.LastName &&
this->Patronymic == other.Patronymic)
return true;
else
return false;
}
};
一个女儿 class 继承了基数 class...这是计算工资并打印出来的绝妙方法...
class Papersworker : public Employee
{
private:
std::string FirstName;
std::string LastName;
std::string Patronymic;
double Salary;
public:
Papersworker() {};
using Employee::Employee;
const std::string Job = "Papersworker";
std::map<std::string, double> Coefficient =
{
{"Director", 4.2},
{"Papersworker", 1.2},
{"Guardian", 1.5},
{"Programmer", 2.5}
};
void ChangeCoefficient(std::string Job, double NewCoefficient)
{
Coefficient[Job] = NewCoefficient;
}
void ChangeNameSalary(std::string FirstName, std::string LastName, std::string Patronymic, double Salary)
{
this->FirstName = FirstName;
this->LastName = LastName;
this->Patronymic = Patronymic;
this->Salary = Salary;
}
void PrintPapersworker()
{
std::cout << "First name\t" << "Lastname\t" << "Patronymic\t" << "Salary\n" << this->FirstName << "\t\t" << this->LastName << "\t" << this->Patronymic << "\t" << this->Salary << "\n" << std::flush;
for (const auto& i : this->Coefficient)
{
std::cout << i.first << " = " << i.second << ";\t" << std::flush;
}
std::cout << "\n------------------------------------------------------------\n" << std::flush;
}
double GetSalary(double Salary, std::string Job)
{
return Salary * this->Coefficient[Job];
}
};
精彩的 int main() 部分。
int main()
{
Papersworker pwr;
double sr = 0.0;
std::cout << "\nEnter director's salary\t" << std::flush; std::cin >> sr;
std::cout << "\nSalary\t" << pwr.GetSalary(sr, "Director");
return 0;
}
如果您看到一些不好的需要优化的地方,请不要介意回复。 ._.我不明白 classes 建筑技巧是怎么回事。 https://pastebin.com/p7HXaX80 P. S. 我的作业强制使用私人名字,姓氏,父名,薪水...... P. S. S. 但是,我现在使用 Visual Studio 2022 Preview 和最新的 C++。 https://imgur.com/a/N8cDK3n
Papersworker
的所有私有成员都隐藏了 Employee
class Papersworker : public Employee {
private:
std::string FirstName;
std::string LastName;
std::string Patronymic;
double Salary;
// ...
};
试试这个:Compiler Explorer(未测试,因为我现在很着急,回来时会看看)
另外,be careful 和 std::map::operator []
您的代码将无法编译,因为 sr
变量未定义。
在 main() 的语句 std::cin >> sr;
中使用它之前定义 double sr;
并且(至少)程序将编译并与用户交互。
int main() {
Papersworker pwr;
std::cout << "\nEnter director's salary\t" << std::flush;
double sr; // <- your missed variable
std::cin >> sr;
std::cout << "\nSalary\t" << pwr.GetSalary(sr, "Director");
}
程序使用输入 10 打印:
Enter director's salary 10
Salary 42
program does not print out anything
如果您将 _getch()
更改为 getch()
,您的程序(您提供的 pastebin link)编译成功并连续打印,可以看到 here。但出于某种原因,它永远持续下去。由于您提供的 link 有大约 500 行代码,所以我没有查看为什么条件没有被破坏(或者程序是否有任何未定义的行为)。也许您可以缩小问题范围并再次编辑您的问题。