如何解决Turbo C中C++含switch函数无法内联展开的错误?
问题解决:Turbo C中"functions containing switch are not expanded inline"错误
错误原因
Turbo C的老式C++编译器会将类内直接定义的成员函数默认按inline函数处理,但它不支持包含switch语句的函数被inline展开。你的display_1函数在类内定义且包含多层switch结构,触发了这个编译限制。
直接解决方法
把包含switch的成员函数(比如display_1)的定义移到类外部,这样编译器就不会默认将其作为inline函数处理,自然消除该错误。
修改后的完整可编译代码
#include<iostream.h> #include<conio.h> #include<string.h> // 新增memset所需头文件 class hostel_mangt { public: int x,h,id,rc,hd; char name[15],dol[10]; void oprt_1(); // 类内仅声明函数 void display_1(); }; // 类外定义oprt_1函数 void hostel_mangt::oprt_1() { cout<<"do u want to see or update room's ?"<<endl; cout<<"enter 1 to see and 0 to do operations = "<<endl; cin>>h; } // 类外定义display_1函数(包含switch,不再被默认inline) void hostel_mangt::display_1() { if(h==1) { // char数组不能直接和NULL比较,检查首字符是否为空判断房间状态 if (name[0] == '\0') { cout<<"room is empty "<<endl; } else { cout<<"id = "<<id<<endl; cout<<"name = "<<name<<endl; cout<<"date of leaving = "<<dol<<endl; } } // 修复原代码缺失的if闭合括号 else { cout<<" 1. Update the room member and its data "<<endl; cout<<" 2. delete the room member and its data "<<endl; cout<<"enter choice = " ; cin>>x; switch(x) { case 1: { // 修复原代码多余的分号和<<符号 cout<<"what do u want to update ? "<<endl; cout<<" 1. name "<<endl; cout<<" 2. date of leaving"<<endl; cin>>rc; switch(rc) { case 1: { cout<<"enter new name = "<<endl; cin>>name; break; // 新增break避免case穿透 } case 2: { cout<<"enter updated date of leaving = "; cin >> dol; // 修复原代码未声明的date变量,改为类内的dol break; } } break; } case 2: { cout<<"what do you want to be deleted = "; cout<<" 1. name "<<endl; cout<<" 2. date of leaving "<<endl; cin>>hd; switch(hd) { case 1: { // 修复char数组赋值方式:用memset清空数组 memset(name, 0, sizeof(name)); break; } case 2: { memset(dol, 0, sizeof(dol)); break; } } break; } } } } int main() { int i,c; clrscr(); hostel_mangt hm[10]; for(i=0;i<10;i++) { hm[i].oprt_1(); // 修复对象数组的成员访问方式,需加索引[i] hm[i].display_1(); cout<<"do u want to continue ? "<<endl<<"if yes enter 1"<<endl; cin>>c; if(c!=1) { break; } } getch(); return 0; // 修复原代码的冒号为分号 }
额外修复的代码问题
除核心错误外,原代码还有多处语法/逻辑问题,上述示例已一并修复:
- 修复
display_1函数中if语句的括号不匹配问题 - 移除main函数中多余的
public关键字 - 修复对象数组的成员访问方式(需通过索引
[i]访问) - 修正char数组的空值判断与赋值逻辑:不能直接用
==NULL,改为检查首字符或用memset清空 - 修复cout语句中的语法错误(多余的分号和
<<符号) - 补充switch case中缺失的
break,避免逻辑穿透 - 修复未声明的
date变量,改为类内定义的dol - 修复main函数中
return 0后的标点错误
内容的提问来源于stack exchange,提问作者Aaryan_mb
相关产品推荐
相关产品推荐

