[英]Adding Pointer results in error to class error overloaded member function not found
我的代碼有些問題。 特別:
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
我得到的錯誤如下:
請幫忙
在Customer
定義之前,您需要向Inventory
前向聲明。
class Inventory;
class Customer {
//....
};
class Inventory {
//....
};
關於代碼的一些注意事項:
通過const
引用傳遞復雜的數據類型 - 例如 - bool addCart( const Food& f );
而不是bool addCart( Food f );
您使用不合格的string
表明您using namespace std;
標題中的某個地方。 那不行,你應該,至少在標題中,沒有using
指令,而是完全限定類型std::string
,它也應該通過引用傳遞
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.