错误C2601:“;“名称”:本地函数定义是非法的 我是C++新手,学习继承和多态。我们需要编写一个员工项目,该项目有4种类型的员工(BasePlusCommission、CommissionOneEmployee、Salaried和TipWorker)。我的项目有一个main()类,我对每种类型的员工使用switch方法。我被困在TipWorker上,我们必须在那里做。这是我到目前为止得到的 int main() { void virtualViaPointer(const Employee * const); { cout << "Enter First Name: " << endl; cin >> firstName; cout << "Enter Last Name: " << endl; cin >> lastName; cout << "Enter SSN: " << endl; cin >> SSN; if (SSN.length() == 9) { SSN = true; } else { cout << "Please enter SSN again with 9 digits only:" << endl; cin >> SSN; } cout << "Enter wages: " << endl; cin >> wage; cout << "Enter hours: " << endl; cin >> hours; cout << "Enter tips: " << endl; cin >> tips; TipWorker employee4(firstName, lastName, SSN, wage, hours, tips); employee4.print(); cout << fixed << setprecision(2); vector < Employee * > employees(1); employees[0] = &employee4; cout << "Employee processed polymorphically via dynamic binding: \n\n"; cout << "Virtual function calls made off base-class pointers:\n\n"; for (const Employee *employeePtr : employees) virtualViaPointer(employeePtr); void virtualViaPointer(const Employee * const baseClassPtr) { baseClassPtr->print(); cout << "\nEarned $" << baseClassPtr->earnings() << "\n\n"; } break; } } intmain() { void virtualViaPointer(const Employee*const); { 不能直呼其名; 库特姓氏; coutssn; 如果(SSN.length()=9) { SSN=真; } 其他的 { coutssn; } 工资; 休息时间; 小贴士; TipWorker员工4(名字、姓氏、SSN、工资、工时、小费); employee4.print(); cout

错误C2601:“;“名称”:本地函数定义是非法的 我是C++新手,学习继承和多态。我们需要编写一个员工项目,该项目有4种类型的员工(BasePlusCommission、CommissionOneEmployee、Salaried和TipWorker)。我的项目有一个main()类,我对每种类型的员工使用switch方法。我被困在TipWorker上,我们必须在那里做。这是我到目前为止得到的 int main() { void virtualViaPointer(const Employee * const); { cout << "Enter First Name: " << endl; cin >> firstName; cout << "Enter Last Name: " << endl; cin >> lastName; cout << "Enter SSN: " << endl; cin >> SSN; if (SSN.length() == 9) { SSN = true; } else { cout << "Please enter SSN again with 9 digits only:" << endl; cin >> SSN; } cout << "Enter wages: " << endl; cin >> wage; cout << "Enter hours: " << endl; cin >> hours; cout << "Enter tips: " << endl; cin >> tips; TipWorker employee4(firstName, lastName, SSN, wage, hours, tips); employee4.print(); cout << fixed << setprecision(2); vector < Employee * > employees(1); employees[0] = &employee4; cout << "Employee processed polymorphically via dynamic binding: \n\n"; cout << "Virtual function calls made off base-class pointers:\n\n"; for (const Employee *employeePtr : employees) virtualViaPointer(employeePtr); void virtualViaPointer(const Employee * const baseClassPtr) { baseClassPtr->print(); cout << "\nEarned $" << baseClassPtr->earnings() << "\n\n"; } break; } } intmain() { void virtualViaPointer(const Employee*const); { 不能直呼其名; 库特姓氏; coutssn; 如果(SSN.length()=9) { SSN=真; } 其他的 { coutssn; } 工资; 休息时间; 小贴士; TipWorker员工4(名字、姓氏、SSN、工资、工时、小费); employee4.print(); cout,c++,pointers,inheritance,polymorphism,C++,Pointers,Inheritance,Polymorphism,不能在其他函数中定义一个函数。任何函数定义都应在任何其他函数定义之外 将virtualViaPointer的定义置于main的主体之外。您可以在函数中声明函数,但不能定义该函数。但是,您可以在函数中具有本地结构/类或lambda: #include <iostream> void f() { void print(); // mostly useless - maybe a most vexing parse error struct Print { static v

不能在其他函数中定义一个函数。任何函数定义都应在任何其他函数定义之外


virtualViaPointer
的定义置于
main

的主体之外。您可以在函数中声明函数,但不能定义该函数。但是,您可以在函数中具有本地结构/类或lambda:

#include <iostream>
void f()
{
  void print(); // mostly useless - maybe a most vexing parse error
  struct Print {
    static void apply(const char* s) { std::cout << s << '\n'; }
  }
  Print::apply("Hello");
  auto lambda = [] (const char* s) { std::cout << s << '\n'; };
  lambda("World");
}
#包括
void f()
{
void print();//基本上没有用-可能是一个最令人烦恼的分析错误
结构打印{

静态空格应用(const char s){STD::CUT它是很自我解释的。你定义了ValualViaPoTrin内部函数main,而在C++中,你不能有局部函数(不包括lambdas,但是它们有不同的语法)。可能是重复的,那么我该如何修复这个错误呢?因为我不太明白你的意思。没关系,我想我现在已经搞定了。我已经搞定了。但是谢谢你的想法。我现在已经搞定了。当我把它放在我的主()。但是我怎样才能增加收入20%。你能帮我吗?@Phu Nguyen我不知道你怎么计算收入,但它不足以乘以1.2吗?哈哈,我问了这么多愚蠢的问题。我实际上解决了它。我乘以1.2,但用错了地方。谢谢你的回答:)
#include <iostream>
void f()
{
  void print(); // mostly useless - maybe a most vexing parse error
  struct Print {
    static void apply(const char* s) { std::cout << s << '\n'; }
  }
  Print::apply("Hello");
  auto lambda = [] (const char* s) { std::cout << s << '\n'; };
  lambda("World");
}