Search code examples
c++vectorenumspush-back

error: no matching function for call to vector::push_back


I'm new to C++ and am trying to use pushback function to store food orders in a vector object so that the food orders can be compiled and repeated back later. But I get the error: no matching function for call to ‘std::vector::push_back(OrderEntry*&)’ when I try to use the pushback function to store the food order.

#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;
};

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 plat for wanton noodles" << std::endl;
    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;

    string choice = getUserOption();
    int qty;
    
    while(true)
    {
        printMenu();

        std::endl (std::cout); 

        if (choice=="a")
        {
            std::cout << "How many plates of chicken rice would you like to order?" << std::endl; 
            OrderType order = OrderType::chickenrice;
            std::endl (std::cout);
            qty = getQuantity();
            std::cout << "Thank you for your order of " << qty << " plates of chicken rice." << std::endl; 
            std::endl (std::cout);
            OrderEntry* food_order = new OrderEntry (qty, order);
            orderQueue.push_back(food_order);
        }
        if (choice=="b")
        {
            std::cout << "How many plates of wanton noodles would you like to order?" << std::endl;
            OrderType order = OrderType::wantonnoodle};
            std::endl (std::cout);
            qty = getQuantity();
            std::cout << "Thank you for your order of " << qty << " plates of wanton noodles." << std::endl; 
            std::endl (std::cout); 
            OrderEntry* food_order = new OrderEntry (qty, order);
            orderQueue.push_back(food_order);
        }
        if (choice=="c")
        {
            std::cout << "How many cups of Coffee would you like to order?" << std::endl; 
            OrderType order = OrderType::coffee};
            std::endl (std::cout);          
            qty = getQuantity();
            std::cout << "Thank you for your order of " << qty << " cups of Coffee." << std::endl; 
            OrderEntry* food_order = new OrderEntry (qty, order);
            orderQueue.push_back(food_order);
        }
        if (choice=="d")
        {
            std::cout << "How many cups of Tea would you like to order?" << std::endl; 
            OrderType order = OrderType::tea};
            std::endl (std::cout);
            qty = getQuantity();
            std::cout << "Thank you for your order of " << qty << " cups of Tea." << std::endl; 
            std::endl (std::cout); 
            OrderEntry* food_order = new OrderEntry (qty, order);
            orderQueue.push_back(food_order);
        }
        if (choice=="e")
        {
            std::cout << "You have ordered: " << std::endl;
            
            for (unsigned int i = 0; i < orderQueue.size(); ++i)
            {
                std::cout << orderQueue[i].qty << std::endl;
            }
            std::endl (std::cout);
        }
        if (choice=="f")
        {
            std::cout << "Thank you and see you again soon." << std::endl;
            return 0;
        }
    }  
}


Solution

  •             OrderEntry* food_order = new OrderEntry (qty, order);
                orderQueue.push_back(food_order);
    

    is wrong because the elements of orderQueue is OrderEntry while you are trying to push OrderEntry* and OrderEntry don't have constructors that accepts OrderEntry*.

    It should be

                OrderEntry food_order = OrderEntry (qty, order);
                orderQueue.push_back(food_order);
    

    or simply

                orderQueue.push_back(OrderEntry (qty, order));