簡體   English   中英

添加指針會導致錯誤,因為找不到類錯誤重載的成員函數

[英]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

我得到的錯誤如下:

  1. 錯誤C2061:語法錯誤:標識符'庫存'錯誤C2061 :(關於bool checkout(Inventory * inv);)
  2. 語法錯誤:標識符'庫存'布爾
  3. Customer :: checkout(Inventory )':'Customer'中找不到重載的成員函數,請參閱'Customer'的聲明....(關於bool Customer :: checkout(Inventory inv))

請幫忙

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM