2016-03-06 84 views
-3

我已經成功地通過我的代碼加強,它是填充陣列,但是當我嘗試提取它告訴我的數據「錯誤的表達必須具有類類型」如何從結構數組中提取數據?

void Inventory::fillInventory(char* buff, int len) 
{ 
using namespace std; 
int i = 0; 
int upcNum = 0; 
string itDesc = ""; 
string itPrice = ""; 
bool itTax = false; 
do 
{ 
    do 
    { 
     // assign upcNum 
     if (buff[i] >= 48 && buff[i] <= 57) 
     { 
      string str = ""; 
      while (buff[i] != 32) 
      { 
       str += buff[i]; 
       i++; 
      } 
      upcNum = stoi(str, nullptr, 10); 
     } 
     // assign itDesc 
     else if (buff[i] >= 97 && buff[i] <= 122) 
     { 
      string str = ""; 
      while (buff[i] != 32) 
      { 
       str += buff[i]; 
       i++; 
      } 
      itDesc = str; 
     } 
     // assign itPrice 
     else if (buff[i] == 36) 
     { 
      string str = ""; 
      while (buff[i] != 32) 
      { 
       str += buff[i]; 
       i++; 
      } 
      itPrice = str; 
     } 
     // assign itTax 
     else if (buff[i] == 78 || buff[i] == 84) 
     { 
      switch (buff[i]) 
      { 
      case 78: 
       itTax = false; 
       break; 
      case 84: 
       itTax = true; 
       break; 
      } 
     } 
     i++; 
    } while (buff[i] != 10 && i < len); 


    // fill struct 
    newItem = new Item; 
    newItem->upc = upcNum; 
    newItem->desc = itDesc; 
    newItem->cost = itPrice; 
    newItem->tax = itTax; 

    if (inInventory < MAX_INV) 
    { 
     inventory[inInventory] = newItem; 
    } 
    else 
    { 
     cout << "Inventory is full..." << endl; 
    } 
    delete newItem; 
} while (i < bufferLength); 
int upcInt = inventory[0].upc; // this is my error 
} 

然而,尼克的解決方案似乎已經工作了。

+0

upcNum是一個int。 – yorTsenoJ

+1

什麼是項目,newItem,庫存? – deviantfan

+0

請發佈[最小測試用例](http://stackoverflow.com/help/mcve)。 –

回答

1
inventory[inInventory] = newItem; 

讓我相信庫存是指向Item s的指針數組。如果是這樣,則需要使用inventory[0]->upc來訪問元素的數據成員,而不是inventory[0].upc,因爲每個元素都是指向Item的指針。

+0

這樣做了!謝謝! – yorTsenoJ