尝试使用 init() 启动我的程序,但它告诉我没有合适的默认构造函数可用
Trying to use init() to start my program but it tells me no appropriate default constructor available
我正在尝试使用 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)
,编译器将不再自动生成默认构造函数。
为了使其工作,您需要手动定义默认构造函数或在实例化 MerkelMain
.
时传递 OrderFood
的实例
您在执行 MerkelMain app{};
时试图使用默认构造函数,但是
您的 MerkelMain
class 没有默认构造函数。
一种可能的方法是添加一个默认构造函数,该构造函数委托给您当前的转换构造函数以从看起来像默认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
{}
我正在尝试使用 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)
,编译器将不再自动生成默认构造函数。
为了使其工作,您需要手动定义默认构造函数或在实例化 MerkelMain
.
OrderFood
的实例
您在执行 MerkelMain app{};
时试图使用默认构造函数,但是
您的 MerkelMain
class 没有默认构造函数。
一种可能的方法是添加一个默认构造函数,该构造函数委托给您当前的转换构造函数以从看起来像默认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
{}