如何解决C++中“请求访问非类类型int的成员printDetails”错误?
错误原因与解决方法
核心错误分析
报错request for member printDetails in book which is of non class type int的直接原因是:遍历书籍列表时,循环变量被错误声明为int类型(而非Book类的引用/对象),导致编译器认定book是整数,无法调用类成员函数printDetails()。
你提供的代码中展示书籍的循环写法是正确的:
for ( auto& book : bookList) { book.printDetails(); }
但实际运行的代码大概率误写成了:
for (int book : bookList) { book.printDetails(); }
这就触发了上述错误。
修正步骤
修正循环变量类型:确保遍历
vector<Book>时,循环变量是Book的引用(避免拷贝、提升效率)或对象:// 推荐写法:auto自动推导类型,或显式声明Book& for (auto& book : bookList) { book.printDetails(); } // 显式声明写法 for (Book& book : bookList) { book.printDetails(); }修复输入逻辑的隐藏问题:代码中存在输入换行符残留的问题,比如输入整数后调用
cin.ignore()仅跳过单个字符,可能导致后续getline读到空字符串。建议统一用cin.ignore(numeric_limits<streamsize>::max(), '\n')清除整行残留字符(需包含<limits>头文件):- 修正后的插入书籍代码片段:
case 2: { Book newBook; string title, publisherRef; int editions; double price; cout << "Enter title: "; cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除之前输入的残留换行 getline(cin, title); newBook.setTitle(title); cout << "Enter editions: "; cin >> editions; newBook.setEditions(editions); cout << "Enter publisher reference: "; cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除整数输入后的换行 getline(cin, publisherRef); newBook.setPublisherRef(publisherRef); cout << "Enter price: "; cin >> price; newBook.setPrice(price); bookList.push_back(newBook); cout << "Book added to the list.\n"; break; } - 记得在文件开头添加头文件:
#include <limits>
- 修正后的插入书籍代码片段:
完整修正后的代码
#include <iostream> #include <string> #include <vector> #include <limits> // 添加头文件处理输入清除 using namespace std; class Book { private: string title; int editions; string publisherRef; double price; public: void setTitle(string t) { title = t; } void setEditions(int e) { editions = e; } void setPublisherRef(string p) { publisherRef = p; } void setPrice(double pr) { price = pr; } string getTitle() const { return title; } int getEditions() const { return editions; } string getPublisherRef() const { return publisherRef; } double getPrice() const { return price; } void printDetails() { cout << "Title: " << title << endl; cout << "Editions: " << editions << endl; cout << "Publisher reference: " << publisherRef << endl; cout << "Price: " << price << endl; } }; int main() { vector<Book> bookList; int choice; do { cout << "Main menu\n"; cout << "1. Book\n"; cout << "0. Exit\n"; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: { int bookChoice; do { cout << "Book submenu\n"; cout << "1. Display\n"; cout << "2. Insert\n"; cout << "0. Back\n"; cout << "Enter your choice: "; cin >> bookChoice; switch (bookChoice) { case 1: { if (bookList.empty()) { cout << "There are no books in the list.\n"; } else { cout << "Book list:\n"; for (auto& book : bookList) { book.printDetails(); } } break; } case 2: { Book newBook; string title, publisherRef; int editions; double price; cout << "Enter title: "; cin.ignore(numeric_limits<streamsize>::max(), '\n'); getline(cin, title); newBook.setTitle(title); cout << "Enter editions: "; cin >> editions; newBook.setEditions(editions); cout << "Enter publisher reference: "; cin.ignore(numeric_limits<streamsize>::max(), '\n'); getline(cin, publisherRef); newBook.setPublisherRef(publisherRef); cout << "Enter price: "; cin >> price; newBook.setPrice(price); bookList.push_back(newBook); cout << "Book added to the list.\n"; break; } case 0: { break; } default: { cout << "Invalid choice. Please try again.\n"; break; } } } while (bookChoice != 0); break; } case 0: { break; } default: { cout << "Invalid choice. Please try again.\n"; break; } } } while (choice != 0); return 0; }
内容的提问来源于stack exchange,提问作者asdo
相关产品推荐
相关产品推荐

