我有一个存储在名为orderQueue
的类中的 vector OrderFood
和在另一个名为processUserOption
的类中的名为MerkelMain
的函数。我正在尝试使用push_back函数使processUserOption
将订单存储在该orderQueue
vector 中。如何将订单存储在orderQueue
类的OrderFood
vector 中?
OrderFood.h
#pragma once
#include <string>
#include <vector>
#include "OrderEntry.h"
#include "CSVReader.h"
class OrderFood
{
public:
OrderFood(std::string filename);
std::vector<OrderEntry> getOrders(OrderType type,
int qty);
private:
std::vector<OrderEntry> orderQueue;
};
默克尔#include <iostream>
#include <vector>
#include "OrderEntry.h"
#include "MerkelMain.h"
#include "OrderFood.h"
MerkelMain::MerkelMain()
{
}
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);
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;
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);
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;
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);
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;
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);
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 << " " << OrderEntry::orderTypeToString(orderQueue[i].orderType) << std::endl;
}
std::endl(std::cout);
}
if (choice == "f")
{
std::cout << "Thank you and see you again soon." << std::endl;
}
}
最佳答案
您可以将class MerkelMain
成为class OrderFood
的 friend
class OrderFood
{
public:
OrderFood(std::string filename);
std::vector<OrderEntry> getOrders(OrderType type,
int qty);
private:
std::vector<OrderEntry> orderQueue;
friend class MerkelMain; // <-- HERE
};
然后委托(delegate)存储在OrderFood
中的MerkelMain
对象class MerkelMain {
private:
Orderfood order; // <-- HERE
...
};
然后在MerkelMain
方法中自由调用该函数。void MerkelMain::processUserOption(std::string choice) {
...
OrderEntry food_order = OrderEntry(qty, order);
order.orderQueue.push_back(std::move(food_order)); // <-- HERE
...
}