错误 C2601:"Name":本地函数定义是非法的
error C2601: "Name": local function definitions are illegal
我是C++新手,学习继承和多态性。我们需要编写一个有4种类型的员工(BasePlusCommission,CommisisonEmployee,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: nn";
cout << "Virtual function calls made off base-class pointers:nn";
for (const Employee *employeePtr : employees)
virtualViaPointer(employeePtr);
void virtualViaPointer(const Employee * const baseClassPtr)
{
baseClassPtr->print();
cout << "nEarned $" << baseClassPtr->earnings() << "nn";
}
break;
}
}
当我运行该项目时,我出现了这个错误:
错误 C2601:"虚拟ViaPointer":本地函数定义为 非法
void virtualViaPointer(const Employee * const baseClassPtr)
{
baseClassPtr->print();
cout << "nEarned $" << baseClassPtr->earnings() << "nn";
}
谁能帮我?非常感谢!
您不能在另一个函数中定义一个函数。任何函数定义都应在任何其他函数定义之外。
将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");
}
注意:本地结构不需要 C++11(此外,调试时可能看起来更好)
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 为什么派生类的这个定义是非法的
- 自我复制、保护和破坏本地函数定义是非法的
- 为什么使用构造函数调用表示法定义指向指针变量的指针是非法的
- 错误 C2601:"Name":本地函数定义是非法的
- 第二阶段的本地函数定义非法
- 错误 C2601:'main':本地函数定义非法 - MS VS 2013 编译器
- C++括号和格式问题(未声明的标识符,本地函数定义是非法的)
- 本地函数定义是非法的
- 使用带有自定义对齐分配器实现的最新g++,使用SSE和-O3选项编译时出现非法指令(核心转储)
- 错误 C2601:'BlackOps2':本地函数定义是非法的
- C++ 代码错误(非法成员初始化、'class'类型重定义、'return':无法转换)
- 为什么指向未定义结构的指针在C和c++中有时是非法的