0

コードに問題があります。具体的には:

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: 構文エラー: 識別子 'Inventory' エラー C2061: (bool checkout(Inventory* inv) について;)
  2. 構文エラー: 識別子 'Inventory' bool
  3. Customer::checkout(Inventory )' : オーバーロードされたメンバー関数が 'Customer' に見つかりません 'Customer' の宣言を参照してください .... (bool Customer::checkout(Inventory inv) について)

助けてください

4

1 に答える 1

2

Inventoryの定義の前に前方宣言が必要ですCustomer

class Inventory;
class Customer {
    //....
};

class Inventory {
    //....
};

コードに関する注意事項:

  • 複雑なデータ型を参照によって渡すconst-たとえば-bool addCart( const Food& f );代わりにbool addCart( Food f );

  • 修飾されていないものを使用することは、ヘッダーのどこかにあるstringことを示唆しています。using namespace std;それはOKではありません。少なくともヘッダーでは、usingディレクティブを持たないでください。むしろ、タイプ-std::stringを完全に修飾する必要があります。これも参照によって渡される必要があります。

于 2012-07-14T23:29:47.067 に答える