尽管具有打印功能,我的Vector仍无法打印

时间:2019-03-24 18:24:58

标签: c++ vector

我正在开发一个程序,该程序将为对象创建具有特定参数的Combo餐类。我将这些存储在向量中。该矢量似乎运行正常,但是当我使用打印功能时,它没有打印任何内容。我需要帮助使打印功能正常运行。

我尝试使用.at()代替getEntree(),但仍未收到任何输出。我在这里阅读了几篇有关从矢量打印的文章,但仍然无法从程序中获得任何输出。

#include <iostream>
#include <string>
#include <vector>
#include "Combo.h"


void fillOrder(vector<Combo>);
void printOrder(vector<Combo>);

int main()
{
    vector<Combo> myOrder;

    fillOrder(myOrder);
    printOrder(myOrder);
}

vector<Combo> newMyOrder;
void fillOrder(vector<Combo> newMyOrder) {
    string entree;
    string side;
    string sideSize;
    string drink;
    string drinkSize;

    cout << "How many meals would you like to order? ";
    int orderSize;
    cin >> orderSize;

    for (int i=0; i < orderSize; i++) {
        cout << "Would you like a Hamburger, Cheeseburger, or chicken?" << endl;
        cin >> entree;
        cout << "Would you like fries, tots, or a salad for your side?" << endl;
        cin >> side;
        cout << "What size would you like your side?" << endl;
        cin >> sideSize;
        cout << "What would you like to drink?" << endl;
        cin >> drink;
        cout << "What size drink would you like?" << endl;
        cin >> drinkSize;

        Combo newMeal(entree, side, sideSize, drink, drinkSize);
        newMyOrder.push_back(newMeal);

    }
}
    void printOrder(vector<Combo>newMyCombo) {
        unsigned int size = newMyCombo.size();

        for (unsigned int i = 0; i < size; i++) {
            cout << "Your Entree is : " << newMyCombo[i].getEntree()<<endl;
            cout << "Your side is : " << newMyCombo[i].getSide()<<endl;
        }
    }

我希望该程序接受订单,然后打印订单摘要,但是我什么都没得到。

1 个答案:

答案 0 :(得分:2)

通过引用

尝试:

void fillOrder(vector<Combo>&);

void fillOrder(vector<Combo>& newMyOrder)

如果您希望打印功能对副本起作用(因为它只是打印订单),请改用void printOrder(const vector<Combo>&);const限定符将为您提供编译保证,即不应修改容器的函数实际上不会这样做。将其作为参考传递给印刷品将确保您在复制整个物件时不会浪费时间和空间。