我的代码遇到了一些问题。具体是:
bool Customer::checkout(Inventory* inv) {
double total = 0;
for( unsigned int i=0; i < _Cart.size(); i++ ) {
total += _Cart[i].price; // price * quantity??
}
if( Customer.balance < total ) {
return false;
}
else {
unsigned int i =0;
for ( i =0; i < inv->_Inv.size(); i++) {//go through inventory, check names, if there is a match,
if (inv->_Inv[i].name == _Cart[i].name) { //decrement the quantity accordingly, if there is no match,
inv->_Inv[i].quant -= _Cart[i].quant;
break;
}
}
if( i == inv->_Inv.size() ) { //you need to push the new food into the inventory vector
inv->_Inv.push_back(_Cart[i]);
return true;
}
}
在我的头文件中,我有:
class Customer {
public:
Customer( string n, int c, double b );
bool addCart( Food f );
bool removeCart( Food f );
void report(); //to do
bool checkout(Inventory* inv); //to do
protected:
string name;
int card;
double balance;
//CreditCard _CC;
vector<Food> _Cart;
};
class Inventory {
public:
vector<Food> _Inv;
vector<Food> _Purchases;
int interval;
void firststock( string fileName );
void showFirststock( string fileName);
void restock( string file, int inter );
void summary(); //to do
我得到的错误如下:
请帮忙
答案 0 :(得分:2)
在Inventory
的定义之前,您需要向Customer
提出前瞻声明。
class Inventory;
class Customer {
//....
};
class Inventory {
//....
};
关于代码的一些注释:
通过const
引用传递复杂数据类型 - 例如 - bool addCart( const Food& f );
而不是bool addCart( Food f );
您使用不合格的string
表示标题中某处有using namespace std;
。那不行,你应该,至少在标题中,没有using
指令,而是完全限定类型 - std::string
,它也应该通过引用传递