gpt4 book ai didi

c++ - 将数据存储在另一个类的 vector 中

转载 作者:行者123 更新时间:2023-12-03 07:02:01 25 4
gpt4 key购买 nike

我有一个 vector orderQueue存储在一个名为 OrderFood 的类中和一个名为 processUserOption 的函数在另一个名为 MerkelMain 的类中.我正在尝试获取 processUserOption将订单存储在 orderQueue使用 push_back 函数的 vector 。如何在 orderQueue 中存储订单OrderFood 的 vector 类(class)?
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;
};
MerkelMain.cpp
#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
...
}

关于c++ - 将数据存储在另一个类的 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64780932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com