C++中调用vector::push_back出现函数匹配错误问题求助
push_back Mismatch Error in Your C++ Order System Hey there! Let's break down why you're seeing that no matching function for call to ‘std::vector::push_back(OrderEntry*&)’ error and get your order system working smoothly.
The Root Cause
Your orderQueue is declared as std::vector<OrderEntry> — this means it stores actual OrderEntry objects, not pointers to them. But in your code, you're creating a pointer with new OrderEntry(...) and trying to push that pointer into the vector. The types don't match, hence the error.
Two Ways to Fix This
Option 1: Store Objects (Recommended for Beginners)
This is the simpler approach, and it avoids memory leaks since you won't have to manually delete objects created with new. Instead of creating pointers, create local objects or temporary objects to push into the vector:
Instead of:
OrderEntry* food_order = new OrderEntry (qty, order); orderQueue.push_back(food_order);
Do this:
// Create a local object and push it OrderEntry food_order(qty, order); orderQueue.push_back(food_order); // Or even shorter, push a temporary object directly orderQueue.push_back(OrderEntry(qty, order));
Option 2: Store Pointers (If You Need Dynamic Allocation)
If you have a specific reason to use pointers (like polymorphism later), change your vector to store pointers:
std::vector<OrderEntry*> orderQueue;
But note: You'll need to manually delete each pointer when you're done with the vector to avoid memory leaks. For a simple order system, this is unnecessary complexity, so stick with Option 1.
Other Fixes for Your Code
Your code has a couple of other issues that will prevent it from working as expected:
- You only get the user's choice once: The
choice = getUserOption();line is outside yourwhile(true)loop, so the program will repeat the first choice forever. Move this line inside the loop, right afterprintMenu(). - Incorrect
std::endlusage:std::endl(std::cout)is not valid syntax. Usestd::cout << std::endl;instead. - Incomplete order summary: When the user selects "e", you only print the quantity, not the food item. Add a helper function to convert
OrderTypeto a string for display.
Full Corrected Code
#include <iostream> #include <string> #include <vector> using namespace std; enum class OrderType {chickenrice, wantonnoodle, coffee, tea}; class OrderEntry { public: OrderEntry( int _qty, OrderType _orderType) : qty(_qty), orderType(_orderType) { } int qty; OrderType orderType; }; // Helper function to convert OrderType to readable string string orderTypeToString(OrderType type) { switch(type) { case OrderType::chickenrice: return "chicken rice"; case OrderType::wantonnoodle: return "wanton noodles"; case OrderType::coffee: return "coffee"; case OrderType::tea: return "tea"; default: return "unknown item"; } } void printMenu() { std::cout << "Welcome to Restaurant Firebird" << std::endl; std::cout << "a . Order a plate of chicken rice" << std::endl; std::cout << "b . Order a plate of wanton noodles" << std::endl; // Fixed typo "plat" -> "plate" std::cout << "c . Order a cup of Coffee" << std::endl; std::cout << "d . Order a cup of Tea" << std::endl; std::cout << "e . Repeat Order" << std::endl; std::cout << "f . Exit Menu" << std::endl; std::cout << "Type an option from a-f" << std::endl; } string getUserOption() { string choice; std::cin >> choice; return choice; } int getQuantity() { int qty; std::cin >> qty; return qty; } int main() { std::vector<OrderEntry> orderQueue; int qty; while(true) { printMenu(); std::cout << std::endl; // Fixed endl usage string choice = getUserOption(); // Moved inside loop to get new choice each time if (choice=="a") { std::cout << "How many plates of chicken rice would you like to order?" << std::endl; OrderType order = OrderType::chickenrice; std::cout << std::endl; qty = getQuantity(); std::cout << "Thank you for your order of " << qty << " plates of chicken rice." << std::endl; std::cout << std::endl; // Push a temporary OrderEntry object orderQueue.push_back(OrderEntry(qty, order)); } else if (choice=="b") { // Used else if to avoid unnecessary checks std::cout << "How many plates of wanton noodles would you like to order?" << std::endl; OrderType order = OrderType::wantonnoodle; std::cout << std::endl; qty = getQuantity(); std::cout << "Thank you for your order of " << qty << " plates of wanton noodles." << std::endl; std::cout << std::endl; orderQueue.push_back(OrderEntry(qty, order)); } else if (choice=="c") { std::cout << "How many cups of Coffee would you like to order?" << std::endl; OrderType order = OrderType::coffee; std::cout << std::endl; qty = getQuantity(); std::cout << "Thank you for your order of " << qty << " cups of Coffee." << std::endl; std::cout << std::endl; orderQueue.push_back(OrderEntry(qty, order)); } else if (choice=="d") { std::cout << "How many cups of Tea would you like to order?" << std::endl; OrderType order = OrderType::tea; std::cout << std::endl; qty = getQuantity(); std::cout << "Thank you for your order of " << qty << " cups of Tea." << std::endl; std::cout << std::endl; orderQueue.push_back(OrderEntry(qty, order)); } else if (choice=="e") { std::cout << "You have ordered: " << std::endl; for (unsigned int i = 0; i < orderQueue.size(); ++i) { // Print both quantity and item name std::cout << orderQueue[i].qty << " x " << orderTypeToString(orderQueue[i].orderType) << std::endl; } std::cout << std::endl; } else if (choice=="f") { std::cout << "Thank you and see you again soon." << std::endl; return 0; } else { std::cout << "Invalid option! Please choose from a-f." << std::endl; std::cout << std::endl; } } }
Key Improvements in the Corrected Code
- Fixed the
push_backtype mismatch by storingOrderEntryobjects instead of pointers - Moved
getUserOption()inside the loop to capture new choices each iteration - Fixed invalid
std::endlsyntax - Added a helper function to display readable order names
- Used
else ifto optimize condition checks - Added handling for invalid user input
- Fixed a typo in the menu ("plat" -> "plate")
内容的提问来源于stack exchange,提问作者ahmadalibin

