C++ 在C+中引用向量+;班
大家好,我有下面的代码。这是一个简单的项目,我只是尝试在C++中练习类,而不是。该程序可以运行,但我唯一的问题是,C++ 在C+中引用向量+;班,c++,function,class,vector,C++,Function,Class,Vector,大家好,我有下面的代码。这是一个简单的项目,我只是尝试在C++中练习类,而不是。该程序可以运行,但我唯一的问题是,printOrder()应该能够循环遍历味道向量,并使用味道中前4个字母的子字符串打印出顺序 现在的问题是向量是空的,所以当我打印订单时。。没有命令。。我知道为什么,因为我传递的向量没有引用类中的私有向量。我不知道如何在课堂上引用向量。有人能帮忙吗?我对这一点很陌生,所以请尽可能详细,可能有代码示例?我可能会破坏这门课的结构,但练习者是完美的 提前谢谢大家 #include "std
printOrder()
应该能够循环遍历味道向量,并使用味道中前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 = "";
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
使用名称空间std;
整数_小=0;
整数=0;
整数=0;
int计数器=1;
int-vecCount=1;
阶级秩序
{
公众:
//违约
订单();
//参数化
顺序(字符串s、字符串flav、向量&f)
{
尺寸=s;
风味=黄;
x=f;
}
//功能
无效getYogurtSize(字符串s)
{循环:
大小;
//If语句
如果(尺寸=“小”)
{number_small++}
否则,如果(大小=“中等”)
{number_medium++}
else if(大小=“大”)
{number_large++}
否则{cout您需要通过引用传递f
。您的代码正在将f
的副本传递给getYogourtFlavors
和printOrder
。这意味着当您在getYogourtFlavors
中修改f
时,更改不会反映在调用函数中
您的方法应该更像这样(注意添加了&
符号):
void getYogurtFlavors(字符串风味、向量和f)
...
作废打印订单(矢量和口味)
你是否考虑过用调试信息和警告编译(例如用<代码> G++-WALL-G/<代码>…如果使用……),那么使用调试器……问题的实质是,有两个F向量,一个在类内,一个在主。当你调用<代码> GyyGuurtTror(香料,F)时,您正在通过一个代码> f>代码>。请阅读关于ValueValueVS和PASS BASIC的参考资料。谢谢你们的信息。我一定会读到的。我以前学过,但是现在我懂了。谢谢你们!像个魅力一样。能推荐一些关于C++的好的阅读吗?我很想学习。但是,我不知道你是如何学习C++的,但是我有一种坏的感觉,那不是一个好的源(循环分支,例子)。在C中,GOTOs的使用是不被禁止的,在C++中也不被禁止,但是它是不推荐的。我的建议?请使用一本好书。
void getYogurtFlavors(string flavor,vector<string>& f)
...
void printOrder(vector<string>& flavors)