引用C ++类中的向量

时间:2014-08-06 04:13:26

标签: c++ function class vector

嘿所有人,所以我有以下代码。它是一个简单的项目,我只是尝试用C ++实践类,而不是。程序半工作,但我唯一的问题是printOrder()应该能够遍历Flavors的矢量并使用调味品中前4个字母的子字符串打印出订单。

现在问题是向量是空的,所以当我打印订单时......没有订单..我知道为什么,因为我传入的向量不引用类中的私有向量。我不知道如何在类中引用向量。有人可以帮忙吗?我是新手,所以请尽可能详细,可能还有代码示例?我可能已经对这个班级的结构进行了抨击,但是练习制作者完美无缺。

提前感谢大家。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>


using namespace std;


    int number_small = 0;
    int number_medium = 0;
    int number_large = 0;
    int counter = 1;
    int vecCount = 1;


    class Order
{



public:
    //Default
    Order();
    //Paramerterized
    Order(string s, string flav,vector <string>& f)
    { 

    size = s;
    flavors = flav;
    x = f;
}

    //Functions

    void getYogurtSize(string s)
    {   LOOP:
            cout << "Please enter small, medium, or large: ";
            cin >> size;
            //If Statement
            if (size == "small")
            {   number_small++;}
            else if (size == "medium")
            {   number_medium++;}
            else if (size == "large")
            {   number_large++;}
            else    {   cout << "enter the correct input!\n\n"; 
            goto LOOP;} }


     void getYogurtFlavors(string flavor,vector<string> f)
    {
        vecCount = 1;
        do
        {

            cout << "\nEnter Flavor " << vecCount << ":";
            cin >> flavor;
            if (flavor == "DONE")
            {
                break;
            }

            f.push_back(flavor);  //Moved after the check to not include DONE
            vecCount++;
        } while ((flavor != "DONE") && (vecCount <= 10));
    }



void printOrder(vector<string> flavors)
{
    cout << "Order " << counter << ": ";
    for (auto i : flavors){
        cout << i.substr(0, 4) << "-";
    }
    cout << "**";
}



private:
   //Private Variables
   string size;
   string flavors;  
   vector <string> x;

};


int _tmain(int argc, _TCHAR* argv[])
{

//Variables
Order ord;
string sz;
string flavor;
string input;
vector <string> f;
const double TAX_RATE = 0.0875;
double subtotal;
double tax;
double total;
const double PRICE_SMALL = 2.19;
const double PRICE_MEDIUM = 3.49;
const double PRICE_LARGE = 4.49;

do
{


    ord.getYogurtSize(sz);
    ord.getYogurtFlavors(flavor, f);
    ord.printOrder(f);


    cout << "\n\nWould you like to add another order? ";
    counter++;
    cin >> input;
    f.clear();

} 


while (input == "yes");
{
    subtotal = (number_small*PRICE_SMALL) + (number_medium*PRICE_MEDIUM) + (number_large*PRICE_LARGE);
    tax = (subtotal*TAX_RATE);
    total = tax + subtotal;
    cout << "Subtotal: \t$" << fixed << setprecision(2) << subtotal << endl;
    cout << "Tax (8.75%):    $" << fixed << setprecision(2) << tax << endl;
    cout << "Total:   \t$" << fixed << setprecision(2) << total << endl << endl;
}


return 0;
}

//Default Constructor
Order::Order(){

size = "";
flavors = "";
}

1 个答案:

答案 0 :(得分:1)

您需要通过引用传递f。您的代码正在将f的副本传递给getYogourtFlavorsprintOrder。这意味着当您在f中修改getYogourtFlavors时,更改不会反映在调用函数中。

您的方法应该更像这样(请注意添加&符号):

void getYogurtFlavors(string flavor,vector<string>& f)
...
void printOrder(vector<string>& flavors)