switch case in protected scope 异常解决

本文介绍了一种在Swift语言中解决UITabBar点击事件的方法,通过在switch-case语句中为每个case添加花括号{}

case添加{ }后,编译错误可解决。

- (void)tabBar:(UITabBar *)tb didSelectItem:(UITabBarItem *)item {

    switch(item.tag) {
        case 0:
        {
        }
            break;
        case 1:
        {
        }
            break;
        default:
            break;
    }
}
D:\cxdownload\kechengsheji(3).cpp|9|warning: scoped enums only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp||In function 'std::string ratingToString(Rating)':| D:\cxdownload\kechengsheji(3).cpp|19|error: 'Rating' is not a class or namespace| D:\cxdownload\kechengsheji(3).cpp|20|error: 'Rating' is not a class or namespace| D:\cxdownload\kechengsheji(3).cpp|21|error: 'Rating' is not a class or namespace| D:\cxdownload\kechengsheji(3).cpp|22|error: 'Rating' is not a class or namespace| D:\cxdownload\kechengsheji(3).cpp|36|warning: defaulted and deleted functions only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp|75|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp|81|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp|91|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp||In constructor 'Book::Book(std::ifstream&)':| D:\cxdownload\kechengsheji(3).cpp|98|error: 'readLine' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp|107|error: 'stoi' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp|119|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp|125|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp|135|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp||In constructor 'DVD::DVD(std::ifstream&)':| D:\cxdownload\kechengsheji(3).cpp|142|error: 'readLine' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp|150|error: 'stoi' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp|163|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp|169|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp|179|warning: override controls (override/final) only available with -std=c++11 or -std=gnu++11 [enabled by default]| D:\cxdownload\kechengsheji(3).cpp||In constructor 'Picture::Picture(std::ifstream&)':| D:\cxdownload\kechengsheji(3).cpp|186|error: 'readLine' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp|194|error: 'stoi' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp|218|error: 'nullptr' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp||In function 'int main()':| D:\cxdownload\kechengsheji(3).cpp|246|error: 'nullptr' was not declared in this scope| D:\cxdownload\kechengsheji(3).cpp|247|error: no matching function for call to 'Book::Book()'| D:\cxdownload\kechengsheji(3).cpp|247|note: candidates are:| D:\cxdownload\kechengsheji(3).cpp|97|note: Book::Book(std::ifstream&)| D:\cxdownload\kechengsheji(3).cpp|97|note: candidate expects 1 argument, 0 provided| D:\cxdownload\kechengsheji(3).cpp|68|note: Book::Book(const Book&)| D:\cxdownload\kechengsheji(3).cpp|68|note: candidate expects 1 argument, 0 provided| D:\cxdownload\kechengsheji(3).cpp|248|error: no matching function for call to 'DVD::DVD()'| D:\cxdownload\kechengsheji(3).cpp|248|note: candidates are:| D:\cxdownload\kechengsheji(3).cpp|141|note: DVD::DVD(std::ifstream&)| D:\cxdownload\kechengsheji(3).cpp|141|note: candidate expects 1 argument, 0 provided| D:\cxdownload\kechengsheji(3).cpp|112|note: DVD::DVD(const DVD&)| D:\cxdownload\kechengsheji(3).cpp|112|note: candidate expects 1 argument, 0 provided| D:\cxdownload\kechengsheji(3).cpp|249|error: no matching function for call to 'Picture::Picture()'| D:\cxdownload\kechengsheji(3).cpp|249|note: candidates are:| D:\cxdownload\kechengsheji(3).cpp|185|note: Picture::Picture(std::ifstream&)| D:\cxdownload\kechengsheji(3).cpp|185|note: candidate expects 1 argument, 0 provided| D:\cxdownload\kechengsheji(3).cpp|156|note: Picture::Picture(const Picture&)| D:\cxdownload\kechengsheji(3).cpp|156|note: candidate expects 1 argument, 0 provided| D:\cxdownload\kechengsheji(3).cpp|253|error: 'm' does not name a type| D:\cxdownload\kechengsheji(3).cpp|260|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|260|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|260|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|260|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|260|error: expected ')' before 'if'| D:\cxdownload\kechengsheji(3).cpp|280|error: 'm' does not name a type| D:\cxdownload\kechengsheji(3).cpp|286|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|286|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|286|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|286|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|286|error: expected ')' before 'if'| D:\cxdownload\kechengsheji(3).cpp|291|error: 'm' does not name a type| D:\cxdownload\kechengsheji(3).cpp|298|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|298|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|298|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|298|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|298|error: expected ')' before 'if'| D:\cxdownload\kechengsheji(3).cpp|304|error: 'm' does not name a type| D:\cxdownload\kechengsheji(3).cpp|312|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|312|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|312|error: expected ';' before 'if'| D:\cxdownload\kechengsheji(3).cpp|312|error: expected primary-expression before 'if'| D:\cxdownload\kechengsheji(3).cpp|312|error: expected ')' before 'if'| D:\cxdownload\kechengsheji(3).cpp|317|error: 'm' does not name a type| D:\cxdownload\kechengsheji(3).cpp|320|error: expected ';' before 'break'| D:\cxdownload\kechengsheji(3).cpp|320|error: expected primary-expression before 'break'| D:\cxdownload\kechengsheji(3).cpp|320|error: expected ';' before 'break'| D:\cxdownload\kechengsheji(3).cpp|320|error: expected primary-expression before 'break'| D:\cxdownload\kechengsheji(3).cpp|320|error: expected ')' before 'break'| D:\cxdownload\kechengsheji(3).cpp|326|error: ISO C++ forbids declaration of 'm' with no type [-fpermissive]| D:\cxdownload\kechengsheji(3).cpp|326|error: range-based 'for' loops are not allowed in C++98 mode| D:\cxdownload\kechengsheji(3).cpp|327|error: base operand of '->' is not a pointer| D:\cxdownload\kechengsheji(3).cpp|329|error: base operand of '->' is not a pointer| D:\cxdownload\kechengsheji(3).cpp|345|error: 'it' does not name a type| ||More errors follow but not being shown.| ||Edit the max errors limit in compiler options...| ||=== Build failed: 50 error(s), 11 warning(s) (0 minute(s), 1 second(s)) ===| 有这些报错改一下
最新发布
06-26
你遇到的错误主要是由于编译器默认使用的是 **C++98** 标准,而你的代码中使用了 C++11 的特性(如 `nullptr`、`override`、范围 for 循环等),所以导致了一系列语法错误。 --- ## ✅ 解决方案 ### 1. 启用 C++11 标准 你需要在编译时添加 `-std=c++11` 编译选项。如果你是在命令行下编译: ```bash g++ -std=c++11 your_file.cpp -o your_program ``` 如果你使用的是 IDE(如 Code::Blocks、Dev-C++ 等),请按以下步骤操作: #### Dev-C++ 设置方法: - 打开项目或文件 - 点击菜单栏:`项目(Project)` -> `项目选项(Project Options)` - 在 `参数(Parameters)` 标签下选择 `C++ 编译器(C++ Compiler)` - 在空白处输入: ``` -std=c++11 ``` --- ### 2. 添加缺失的函数声明 报错提示找不到 `readLine()` 和 `stoi()`,我们需要做以下修正: - `readLine()` 函数必须提前声明。 - `stoi()` 是 `<string>` 中的标准函数,需要包含头文件并使用命名空间。 --- ## ✅ 修改后的完整修复版本代码如下: ```cpp #include <iostream> #include <fstream> #include <vector> #include <string> #include <algorithm> #include <sstream> using namespace std; // 声明 readLine 函数 string readLine(ifstream& in); // 媒体评级枚举 enum class Rating { UNRATED, GENERAL, ADULT, CHILD }; // 将枚举转换为字符串 string ratingToString(Rating r) { switch (r) { case Rating::UNRATED: return "未评级"; case Rating::GENERAL: return "一般"; case Rating::ADULT: return "成人"; case Rating::CHILD: return "儿童"; default: return "未知"; } } // 基类 Media class Media { protected: string id; string title; string author; Rating rating; public: virtual ~Media() = default; virtual void display() const = 0; virtual void input(); bool operator==(const string& otherId) const { return id == otherId; } string getId() const { return id; } string getTitle() const { return title; } string getAuthor() const { return author; } Rating getRating() const { return rating; } virtual void saveToFile(ofstream& out) const = 0; static Media* loadFromFile(ifstream& in); }; void Media::input() { cout << "请输入编号: "; cin >> id; cout << "请输入标题: "; cin.ignore(); getline(cin, title); cout << "请输入作者: "; getline(cin, author); int r; cout << "请输入评级(0:未评级, 1:一般, 2:成人, 3:儿童): "; cin >> r; rating = static_cast<Rating>(r); } // 图书类 class Book : public Media { private: string publisher; string isbn; int pages; public: void display() const override { cout << "图书 [编号=" << id << ", 标题=\"" << title << "\", 作者=\"" << author << "\"" << ", 评级=" << ratingToString(rating) << ", 出版社=\"" << publisher << "\", ISBN=\"" << isbn << "\", 页数=" << pages << "]" << endl; } void input() override { Media::input(); cout << "请输入出版社: "; cin >> publisher; cout << "请输入ISBN号: "; cin >> isbn; cout << "请输入页数: "; cin >> pages; } void saveToFile(ofstream& out) const override { out << "BOOK\n" << id << "\n" << title << "\n" << author << "\n" << static_cast<int>(rating) << "\n" << publisher << "\n" << isbn << "\n" << pages << "\n"; } Book(ifstream& in) { id = readLine(in); title = readLine(in); author = readLine(in); int r; in >> r; in.ignore(); rating = static_cast<Rating>(r); publisher = readLine(in); isbn = readLine(in); pages = stoi(readLine(in)); } }; // 视频光盘类 class DVD : public Media { private: string producer; int year; int duration; public: void display() const override { cout << "视频光盘 [编号=" << id << ", 标题=\"" << title << "\", 作者=\"" << author << "\"" << ", 评级=" << ratingToString(rating) << ", 出品人=\"" << producer << "\", 年份=" << year << ", 时长=" << duration << "分钟]" << endl; } void input() override { Media::input(); cout << "请输入出品人: "; cin >> producer; cout << "请输入出品年份: "; cin >> year; cout << "请输入视频时长(分钟): "; cin >> duration; } void saveToFile(ofstream& out) const override { out << "DVD\n" << id << "\n" << title << "\n" << author << "\n" << static_cast<int>(rating) << "\n" << producer << "\n" << year << "\n" << duration << "\n"; } DVD(ifstream& in) { id = readLine(in); title = readLine(in); author = readLine(in); int r; in >> r; in.ignore(); rating = static_cast<Rating>(r); producer = readLine(in); year = stoi(readLine(in)); duration = stoi(readLine(in)); } }; // 图画类 class Picture : public Media { private: string country; int width; int height; public: void display() const override { cout << "图画 [编号=" << id << ", 标题=\"" << title << "\", 作者=\"" << author << "\"" << ", 评级=" << ratingToString(rating) << ", 国籍=\"" << country << "\", 尺寸=" << width << "x" << height << "cm]" << endl; } void input() override { Media::input(); cout << "请输入国籍: "; cin >> country; cout << "请输入宽度(cm): "; cin >> width; cout << "请输入高度(cm): "; cin >> height; } void saveToFile(ofstream& out) const override { out << "PICTURE\n" << id << "\n" << title << "\n" << author << "\n" << static_cast<int>(rating) << "\n" << country << "\n" << width << "\n" << height << "\n"; } Picture(ifstream& in) { id = readLine(in); title = readLine(in); author = readLine(in); int r; in >> r; in.ignore(); rating = static_cast<Rating>(r); country = readLine(in); width = stoi(readLine(in)); height = stoi(readLine(in)); } }; // 辅助函数:从文件中读取一行 string readLine(ifstream& in) { string line; getline(in, line); if (line.empty()) { // 如果为空行,则继续读取直到有内容 while (getline(in, line)) { if (!line.empty()) break; } } return line; } // 工厂方法加载媒体对象 Media* Media::loadFromFile(ifstream& in) { string type = readLine(in); if (type == "BOOK") return new Book(in); else if (type == "DVD") return new DVD(in); else if (type == "PICTURE") return new Picture(in); return nullptr; } int main() { vector<Media*> mediaList; int choice; do { cout << "\n=== 图书馆媒体库管理系统 ===\n" << "1. 添加物品\n" << "2. 查询物品\n" << "3. 显示所有物品\n" << "4. 编辑物品\n" << "5. 删除物品\n" << "6. 统计信息\n" << "7. 存盘保存\n" << "8. 读取数据\n" << "9. 退出系统\n" << "请选择操作: "; cin >> choice; switch (choice) { case 1: { cout << "选择添加类别:1. 图书 2. 视频光盘 3. 图画\n"; int type; cin >> type; Media* item = nullptr; if (type == 1) item = new Book(cin); else if (type == 2) item = new DVD(cin); else if (type == 3) item = new Picture(cin); if (item != nullptr) { bool exists = false; for (auto m : mediaList) { if (*m == item->getId()) { exists = true; delete item; break; } } if (!exists) { mediaList.push_back(item); cout << "添加成功!\n"; } else { cout << "编号重复,添加失败。\n"; } } break; } case 2: { cout << "查询方式:1. 按标题 2. 按编号 3. 按类别\n"; int subChoice; cin >> subChoice; bool found = false; if (subChoice == 1) { string title; cout << "请输入标题: "; cin.ignore(); getline(cin, title); for (auto m : mediaList) { if (m->getTitle() == title) { m->display(); found = true; } } if (!found) cout << "该标题不存在!\n"; } else if (subChoice == 2) { string id; cout << "请输入编号: "; cin >> id; for (auto m : mediaList) { if (m->getId() == id) { m->display(); found = true; break; } } if (!found) cout << "该编号不存在!\n"; } else if (subChoice == 3) { string category; cout << "请输入类别 (book/dvd/picture): "; cin >> category; transform(category.begin(), category.end(), category.begin(), ::tolower); for (auto m : mediaList) { if ((category == "book" && dynamic_cast<Book*>(m)) || (category == "dvd" && dynamic_cast<DVD*>(m)) || (category == "picture" && dynamic_cast<Picture*>(m))) { m->display(); found = true; } } if (!found) cout << "该类别没有物品!\n"; } break; } case 3: for (auto m : mediaList) { m->display(); } break; case 4: { string id; cout << "请输入要编辑的编号: "; cin >> id; bool found = false; for (auto& m : mediaList) { if (m->getId() == id) { cout << "请重新输入新信息:\n"; m->input(); found = true; break; } } if (!found) cout << "该编号不存在!\n"; break; } case 5: { if (mediaList.empty()) { cout << "物品库为空!\n"; break; } string id; cout << "请输入要删除的编号: "; cin >> id; auto it = remove_if(mediaList.begin(), mediaList.end(), [&](Media* m) { return m->getId() == id; }); if (it != mediaList.end()) { delete *it; mediaList.erase(it, mediaList.end()); cout << "删除成功!\n"; } else { cout << "该编号不存在!\n"; } break; } case 6: { int bookCount = 0, dvdCount = 0, pictureCount = 0; for (auto m : mediaList) { if (dynamic_cast<Book*>(m)) bookCount++; else if (dynamic_cast<DVD*>(m)) dvdCount++; else if (dynamic_cast<Picture*>(m)) pictureCount++; } cout << "总物品数: " << mediaList.size() << endl; cout << "图书数量: " << bookCount << endl; cout << "视频光盘数量: " << dvdCount << endl; cout << "图画数量: " << pictureCount << endl; break; } case 7: { ofstream out("library.dat"); for (auto m : mediaList) { m->saveToFile(out); } out.close(); cout << "数据已保存到文件。\n"; break; } case 8: { ifstream in("library.dat"); if (!in.is_open()) { cout << "无法打开文件。\n"; break; } mediaList.clear(); while (in.peek() != EOF) { Media* m = Media::loadFromFile(in); if (m) mediaList.push_back(m); } in.close(); cout << "数据已从文件加载。\n"; break; } case 9: cout << "退出系统。\n"; break; default: cout << "无效选择,请重试。\n"; } } while (choice != 9); // 清理内存 for (auto m : mediaList) delete m; return 0; } ``` --- ##
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值