尝试使用init启动程序,但它告诉我没有合适的默认构造函数

问题描述

我正在尝试使用init()启动程序,但是它告诉我main.cpp的这一行MerkelMain app{};没有“合适的认构造函数”。我需要添加什么才能使其正常工作?我想念一个构造函数吗?

main.cpp

#include "OrderEntry.h"
#include "MerkelMain.h"
#include "CSVReader.h"

int main()
{
    MerkelMain app{};
    app.init();
}

MerkelMain.cpp

#include <iostream>
#include <vector>
#include "OrderEntry.h"
#include "MerkelMain.h"
#include "OrderFood.h"

MerkelMain::MerkelMain(OrderFood _orders)
: orders (_orders)
{

}

void MerkelMain::init()
{
    std::string input;

    while (true)
    {
        printMenu();    
        input = getUserOption();
        processUserOption(input);
    }
}

void MerkelMain::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 << "------------------------------------" << std::endl;
    std::cout << "Type an option from a-f" << std::endl;
}


std::string MerkelMain::getUserOption()
{
    std::string choice;
    std::cin >> choice;
    return choice;
}

int MerkelMain::getQuantity()
{
    int qty;
    std::cin >> qty;
    return qty;
}

void MerkelMain::processUserOption(std::string choice)
{
   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;
        int qty = getQuantity();
        std::endl(std::cout);
        std::cout << "Thank you for your order of " << qty << " plates of Chicken Rice." << std::endl;
        std::endl(std::cout);
        OrderEntry food_order = OrderEntry(qty,order);
        orders.orderQueue.push_back(std::move(food_order));
    }
    if (choice == "b")
    {
        std::cout << "How many plates of wanton noodles would you like to order?" << std::endl;
        OrderType order = OrderType::wantonnoodle;
        int qty = getQuantity();
        std::endl(std::cout);
        std::cout << "Thank you for your order of " << qty << " plates of Wanton Noodles." << std::endl;
        std::endl(std::cout);
        OrderEntry food_order = OrderEntry(qty,order);
        orders.orderQueue.push_back(std::move(food_order));
    }
    if (choice == "c")
    {
        std::cout << "How many cups of Coffee would you like to order?" << std::endl;
        OrderType order = OrderType::coffee;
        int qty = getQuantity();
        std::endl(std::cout);
        std::cout << "Thank you for your order of " << qty << " cups of Coffee." << std::endl;
        OrderEntry food_order = OrderEntry(qty,order);
        orders.orderQueue.push_back(std::move(food_order));
    }
    if (choice == "d")
    {
        std::cout << "How many cups of Tea would you like to order?" << std::endl;
        OrderType order = OrderType::tea;
        int qty = getQuantity();
        std::endl(std::cout);
        std::cout << "Thank you for your order of " << qty << " cups of Tea." << std::endl;
        std::endl(std::cout);
        OrderEntry food_order = OrderEntry(qty,order);
        orders.orderQueue.push_back(std::move(food_order));
    }
    if (choice == "e")
    {
        std::cout << "You have ordered: " << std::endl;

        for (unsigned int i = 0; i < orders.orderQueue.size(); ++i)
        {
            std::cout << orders.orderQueue[i].qty << " " << OrderEntry::orderTypetoString(orders.orderQueue[i].orderType) << std::endl;
        }
        std::endl(std::cout);
    }
    if (choice == "f")
    {
        std::cout << "Thank you and see you again soon." << std::endl;
        
    }
}

MerkelMain.h

#pragma once
#include <iostream>
#include <vector>
#include "CSVReader.h"
#include "OrderEntry.h"
#include "OrderFood.h"

class MerkelMain
{
    public:
        MerkelMain(OrderFood _orders);
        void init();
    private:
        void printMenu();
        std::string getUserOption();
        int getQuantity();
        void processUserOption(std::string choice);

        OrderFood orders;
        OrderFood orderFood{"Rest_Order.csv"};
};

解决方法

main函数中,您试图通过调用其默认构造函数来实例化MerkelMain的实例。

MerkelMain app{};

但是,由于您拥有用户定义的构造函数MerkelMain(OrderFood _orders),因此编译器将不再自动生成默认的构造函数。

为了使其正常工作,您将需要在实例化OrderFood时手动定义默认构造函数或传递MerkelMain的实例。

,

您尝试在使用MerkelMain app{};时使用默认构造函数,但是 您的MerkelMain类没有默认的构造函数。

一种可能方法是添加一个默认构造函数,该构造函数委派给当前的转换构造函数,以从看起来像默认csv的文件中进行读取。

.hpp

class MerkelMain
{
    public:
        MerkelMain();                    // added default constructor
        MerkelMain(OrderFood _orders);   // leave your current converting ctor as-is
        void init();
    private:
        void printMenu();
        std::string getUserOption();
        int getQuantity();
        void processUserOption(std::string choice);

        OrderFood orders;
        OrderFood orderFood{"Rest_Order.csv"}; // this should probably be removed
};

.cpp

// add this without changing the current converting constructor:
MerkelMain::MerkelMain() :
    MerkelMain(OrderFood{"Rest_Order.csv"}) // delegate to the converting constructor
{}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...