【发布时间】:2020-10-19 20:38:33
【问题描述】:
我的班级正在创建一个迷你商店,我拥有三个可以工作的主要功能,main.cpp、items.cpp、items.h。但是 shoppingcart.cpp 和 shoppinfcart.h 一直给我带来问题,因为我试图从 main.cpp 传递参数,例如 (sc1.add(&a) 到 shoppingcart.h 和 shoppingcart.cpp 中的函数.
我附上了我的代码副本。任何帮助将不胜感激。
Item.h
#ifndef Items_hpp
#define Items_hpp
#include <string>
#include <iostream>
using namespace std;
class Items {
private:
string ItemName;
float ItemPrice;
int ItemQuant;
public:
Items();
Items(string in, float ip, int iq);
void Name(string in);
string entername();
void CalcPrice( float ip);
float enterprice();
void CalcQuant(int iq);
int enterquant();
};
#endif
Items.cpp
#include "Items.h"
Items::Items()
:ItemName("")
,ItemPrice(0)
,ItemQuant(0)
{
}
Items::Items(string in, float ip, int iq)
:ItemName(in)
,ItemPrice(ip)
,ItemQuant(iq)
{
}
void Items:: CalcPrice(float ip)
{
ItemPrice = ip;
}
void Items::CalcQuant(int iq)
{
ItemQuant = iq;
}
void Items:: Name(std::string in)
{
ItemName = in;
}
float Items:: enterprice()
{
return ItemPrice;
}
int Items:: enterquant()
{
return ItemQuant;
}
string Items:: entername()
{
return ItemName;
}
将项目添加到向量的代码的 sn-p ShoppingCart.cpp
#include "ShoppingCart.h"
#include <vector>
void ShoppingCart ::add(&its)
{
shopitems.push_back(&its);
}
ShoppingCart.h
#ifndef ShoppingCart_hpp
#define ShoppingCart_hpp
#include <iostream>
#include <string>
#include <vector>
#include "Items.h"
using namespace std;
class ShoppingCart
{
private:
vector <const char its> shopitems;
string cname; //customers name
string cdate; // todays date
public:
ShoppingCart();
ShoppingCart(string cname);
void add( char *its);
};
#endif
一个主要的例子 main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "Items.h"
#include "ShoppingCart.h"
using namespace std;
int main() {
char choice;
int itemsbuy;
float org = .75;
float appl = .25;
float pear = 1.00;
float bana = .85;
float grape = 6.00;
float oatmlk = 5.00;
float almmlk = 6.00;
float cashmlk= 5.00;
float soymlk = 4.00;
float cocomlk = 3.00;
float twopermlk = 3.00;
float vitdmlk = 4.00;
float fatmlk = 4.00;
float goatmlk = 6.00;
float rasinbran = 4.00;
float granola = 5.00;
float honeybunches = 6.00;
float twix = 4.00;
float honey = 5.00;
float yogurt = 1.50;
float cashyog = 2.00;
float eggs = 1.80;
float vegeggs = 3.00;
float cheese = 2.00;
float alcheese = 3.00;
int itemop;
int itemno;
int productcounter = 0;
int productsum= 0;//might need.
cout << "Welcome to J&S Breakfast Grocery store!"<<endl;
cout << "--------------------------------------"<<endl;
cout << "How many items will you be purchasing today?"<<endl;
cout << endl;
cin >> itemsbuy;
ShoppingCart sc1; //**intializing "shoppingcart.h"**//
for (int i = 0; i < itemsbuy; i++) {
cout << "Menu:"<<endl;
cout << "---------------------------"<<endl;
cout << "A. Fruits"<<endl;
cout << "B. Non-Diary"<<endl;
cout << "C. Diary" <<endl;
cout << "D. Cereal" <<endl;
cout << "E. Other"<<endl;
cout << "Please select your type of items to add to cart"<<endl;
cin >> choice;
cout << endl;
switch (choice) {
case 'A':
case 'a':
cout << "Menu:" <<endl;
cout << "---------------------------"<<endl;
cout << "1.Oranges -$ "<< org<<endl;
cout << "2.Apples -$ "<< appl << endl;
cout << "3.Pears - $"<<pear<<endl;
cout << "4.Bananas - $" << bana << endl;
cout << "5.Grapes - $" << grape << endl;
cout << endl;
cout << "Chooose option"<<endl;
cin >> itemop;
cout << "How Many?"<<endl;
cin >> itemno;
if (itemno > itemsbuy) {
cout << "Error, cannot add more than youre planning to buy. Please start over."<<endl;
break;
}
else {
if (itemop ==1) {
Items a("Oranges" , org, itemno);
productcounter++;
sc1.add(&a); //**trying to pass parameter here**//
if (productcounter == itemsbuy) {
cout<< "Error. You are attempting to buy more than you planned. Please Start over. Thank you."<<endl;
}
}
【问题讨论】:
-
sc1.add(&a);变量a在最近的 {} 定义的范围内是本地的。当它超出范围时,您存储的指针不再有效。 -
vector <const char its> shopitems;应该是vector <Items> shopitems;和void add( char *its);应该是void add( Items it);或者void add( const Items & it); -
而
void add( char *its);应该是void add(const Item& its)并且 Shopping.cpp 上的定义应该是:void ShoppingCart::add(const Item& its) -
您的所有
float项都应标记为static const,以便编译器可以将常量放入可执行文件的只读部分。 -
另外,查找
std::toupper和std::tolower,这样您就可以将输入全部转换为大写或全部小写,并且只使用if语句或一个case语句。跨度>
标签: c++ c++11 parameter-passing