2015-05-27 168 views
1

我是新來的,我在用C++編寫的程序尋求幫助。將函數轉換爲成員函數?

我想轉換下列功能不在一個班,也沒有在main()爲一類庫存的公共職能的成員有沒有參數和返回任何結果我的程序

void PrintInventory(vector<Item*> inventory); 
vector<Item*> AddItemToInventory(vector<Item*> inventory); 
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory); 
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory); 

的。

我一直對4天了,只是想不通,我應該如何改變這些功能是無效的,並具有不破壞整個程序沒有參數...

如果我去完全按指令說什麼這是使空/無參數出的四大功能,我會得到這樣的:

class Inventory 
{ 
public: 
    void PrintInventory() {} 
    void AddItemToInventory() {} 
    void UpdateItemQtyInInventory() {} 
    void RemoveItemFromInventory() {} 

private: 
    vector<Item*> inventory; 
}; 

請給我一個提示。我覺得解決方案必須簡單,但我已經完全陷入了數天。

感謝您的幫助。

編輯: 我仍然收到一堆錯誤,並認爲我需要多說一點我的代碼: 我有一個基類Item和兩個派生類Produce和Book。然後,我有四個功能調用:

// Print all items in the inventory 
void PrintInventory(vector<Item*> inventory); 

// Dialogue to create a new item, then add that item to the inventory 
vector<Item*> AddItemToInventory(vector<Item*> inventory); 

// Dialogue to update the quantity of an item, then update that item in the inventory 
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory); 

// Dialogue to remove a specific item, then remove that specific item from the inventory 
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory); 

接下來是,現在有沒有回報4個轉換功能和無參數原本未類庫存裏面,面前放參數/返回主,我班庫存。

這裏是整個代碼,我認爲這是更好地理解是這樣的:

#include <iostream> 
#include <string> 
#include <vector> 
#include <sstream> 
#include <cstring> 
using namespace std; 

class Item { 
public: 
    void SetName(string nm) 
    { 
     name = nm; 
    }; 
    void SetQuantity(int qnty) 
    { 
     quantity = qnty; 
    }; 
    void SetPrice(int prcInDllrs) //step1 
    { 
     priceInDollars = prcInDllrs; //step1 
    }; 
    virtual void Print() 
    { 
     cout << name << " " << quantity << endl; 
    }; 
    virtual ~Item() 
    { 
     return; 
    }; 
protected: 
    string name; 
    int quantity; 
    int priceInDollars; //step1 
}; 

class Produce : public Item { // Derived from Item class 
public: 
    void SetExpiration(string expir) 
    { 
     expiration = expir; 
    }; 
    void Print() 
    { 
     cout << name << " x" << quantity 
      << " for $" << priceInDollars //step1 
      << " (Expires: " << expiration << ")" 
      << endl; 
    }; 
private: 
    string expiration; 
}; 

//step 2 add derived class Book 
class Book : public Item { // Derived from Item class 
public: 
    void SetAuthor(string authr) //create author function with parameter 
    { 
     author = authr; 
    }; 
    void Print() 
    { 
     cout << name << " x" << quantity 
      << " for $" << priceInDollars //step1 
      << " (Author: " << author << ")" 
      << endl; 
    }; 
private: 
    string author; 
}; 

// Print all items in the inventory 
void PrintInventory(vector<Item*> inventory); 

// Dialogue to create a new item, then add that item to the inventory 
vector<Item*> AddItemToInventory(vector<Item*> inventory); 

// Dialogue to update the quantity of an item, then update that item in the inventory 
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory); 

// Dialogue to remove a specific item, then remove that specific item from the inventory 
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory); 

int main() { 
    vector<Item*> inventory; 
    string usrInptOptn = "default"; 

    while (true) { 
     // Get user choice 
     cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: "; 
     getline(cin, usrInptOptn); 

     // Process user choice 
     if (usrInptOptn.size() == 0) { 
      continue; 
     } 
     else if (usrInptOptn.at(0) == 'p') { 
      PrintInventory(inventory); 
     } 
     else if (usrInptOptn.at(0) == 'a') { 
      inventory = AddItemToInventory(inventory); 
     } 
     else if (usrInptOptn.at(0) == 'u') { 
      inventory = UpdateItemQtyInInventory(inventory); 
     } 
     else if (usrInptOptn.at(0) == 'r') { 
      inventory = RemoveItemFromInventory(inventory); 
     } 
     else if (usrInptOptn.at(0) == 'q') { 
      cout << "\nGood bye." << endl; 
      break; 
     } 
    } 

    return 0; 
} 

class Inventory { 
public: 

    void PrintInventory() { 
     unsigned int i = 0; 
     if (inventory.size() == 0) { 
      cout << "No items to print." << endl; 
     } 
     else { 
      for (i = 0; i<inventory.size(); ++i) { 
       cout << i << " - "; 
       inventory.at(i)->Print(); 
      } 
     }; 
    } 
    void AddItemToInventory() { 


     Produce* prdc; 
     Book* book; //create new pointer object of class book 
     string usrInptName = ""; 
     string usrInptQntyStr = ""; 
     istringstream inSS; 
     int usrInptQnty = 0; 
     string usrInptExpr = ""; 
     int usrInptPrc = 0; //step1 
     string usrInptAuthr = ""; //declare variable 
     string usrInptBookName = ""; 
     int usrInptQntyBook = 0; 
     string usrInptQntyBookStr = ""; 
     string usrInptChoice = " "; 


     //loop user choice and ask again if choice is not valid 
     do { 
      cout << "Enter choice of adding (b)ook or (p)roduce: "; 
      getline(cin, usrInptChoice); 
      if (usrInptChoice != "b" && usrInptChoice != "p") { 
       cout << "Invalid Choice" << endl; 
      } 
     } while (usrInptChoice != "b" && usrInptChoice != "p"); 

     //only ask for inventory type accoring to user input p or b 
     if (usrInptChoice == "p") { 

      cout << "Enter name of new produce: "; 
      getline(cin, usrInptName); 

      cout << "Enter quantity: "; 
      getline(cin, usrInptQntyStr); 
      inSS.str(usrInptQntyStr); 
      inSS >> usrInptQnty; 
      inSS.clear(); 

      cout << "Enter expiration date: "; 
      getline(cin, usrInptExpr); 

      cout << "Enter the price per item : $"; //step1 
      cin >> usrInptPrc; //step1 

      prdc = new Produce; 
      prdc->SetName(usrInptName); 
      prdc->SetQuantity(usrInptQnty); 
      prdc->SetExpiration(usrInptExpr); 
      prdc->SetPrice(usrInptPrc); 

      inventory.push_back(prdc); 
     } 

     if (usrInptChoice == "b") { 
      cout << "Enter name of new book: "; 
      getline(cin, usrInptBookName); 

      cout << "Enter quantity: "; 
      getline(cin, usrInptQntyBookStr); 
      inSS.str(usrInptQntyBookStr); 
      inSS >> usrInptQntyBook; 
      inSS.clear(); 

      cout << "Enter author: "; 
      getline(cin, usrInptAuthr); 

      cout << "Enter the price per item : $"; //step1 
      cin >> usrInptPrc; //step1 

      book = new Book; 
      book->SetName(usrInptBookName); 
      book->SetQuantity(usrInptQntyBook); 
      book->SetAuthor(usrInptAuthr); 
      book->SetPrice(usrInptPrc); 

      inventory.push_back(book); 
     }; 
    } 

    void UpdateItemQtyInInventory() { 
     string usrIndexChoiceStr = ""; 
     unsigned int usrIndexChoice = 0; 
     istringstream inSS; 
     string usrInptQntyStr = ""; 
     int usrInptQnty = 0; 

     if (inventory.size() == 0) { 
      cout << "No items to update." << endl; 
     } 
     else { 
      PrintInventory(); 

      do { 
       cout << "Update which item #: "; 
       getline(cin, usrIndexChoiceStr); 
       inSS.str(usrIndexChoiceStr); 
       inSS >> usrIndexChoice; 
       inSS.clear(); 
      } while (!(usrIndexChoice < inventory.size())); 

      cout << "Enter new quantity: "; 
      getline(cin, usrInptQntyStr); 
      inSS.str(usrInptQntyStr); 
      inSS >> usrInptQnty; 
      inSS.clear(); 

      inventory.at(usrIndexChoice)->SetQuantity(usrInptQnty); 
     }; 
    } 

    void RemoveItemFromInventory() { 
     istringstream inSS; 
     string usrIndexChoiceStr = ""; 
     unsigned int usrIndexChoice = 0; 
     string usrInptQntyStr = ""; 

     if (inventory.size() == 0) { 
      cout << "No items to remove." << endl; 
     } 
     else { 
      PrintInventory(); 

      do { 
       cout << "Remove which item #: "; 
       getline(cin, usrIndexChoiceStr); 
       inSS.str(usrIndexChoiceStr); 
       inSS >> usrIndexChoice; 
       inSS.clear(); 
      } while (!(usrIndexChoice < inventory.size())); 

      inventory.erase(inventory.begin() + usrIndexChoice); 
     }; 
    } 

    private: 
     vector<Item*> inventory; 
}; 

我沒有在Visual Studio中得到任何編譯錯誤,但是當我嘗試建立,有5個錯誤。編譯在Geany程序沒有給我任何的錯誤,但是當我建立了,它說:

undefined reference to `PrintInventory(std::vector<Item*, std::allocator<Item*> >)' 
    undefined reference to `AddItemToInventory(std::vector<Item*, std::allocator<Item*> >)' 
    undefined reference to `UpdateItemQtyInInventory(std::vector<Item*, std::allocator<Item*> >)' 
    undefined reference to `RemoveItemFromInventory(std::vector<Item*, std::allocator<Item*> >)' 
    collect2.exe: error: ld returned 1 exit status 
    Compilation failed. 

我覺得我缺少一些基本的,簡單的事情,但我無法弄清楚。當我說要將函數PrintInventory,AddItemToInventory,UpdateItemQtyInInventory, 和RemoveItemFromInventory轉換爲void/no參數,但是它涉及到哪些函數時,該指令讓我感到困惑。四條定義線?實際功能?都?那麼主要和其他課程呢?我不需要改變那裏的事嗎?

+0

你猜測成員變量'庫存'給你什麼? – WhozCraig

+0

如果我理解你的問題,我的答案是會員變量庫存給我一個在我的主類和其他類中需要的指針向量。例如: int main(){ \t vector inventory; \t string usrInptOptn =「default」; \t而(真){ \t \t //獲取用戶的選擇 \t \t COUT <<「\ n輸入(P)RINT中,(a)DD,(U)PDATE,(R)EMOVE,或(q)中UIT :「; \t \t getline(cin,usrInptOptn); \t \t //處理用戶選擇 \t \t如果(usrInptOptn.size()== 0){ \t \t \t繼續; \t \t} \t \t否則,如果(usrInptOptn.at(0)== 'P'){ \t \t \t PrintInventory(庫存);} \t等 – mhenkes92

+1

它可以讓你通過你的'Inventory'擁有的矢量目的。該「庫存」對象的成員函數可以訪問該向量。因此,他們不需要傳遞給他們的矢量。並且請不要在評論中粘貼代碼牆。 – WhozCraig

回答

0

基本測試:

注意,也有在這裏

void test() 
{ 
    class T_item 
    { 
    public: 
     string name; 
     int quantity; 
     int priceInDollars; 
    }; 

    vector<T_item> inventory; 

    T_item temp; 

    //add item: 
    temp.name = "name1"; 
    temp.quantity = 1; 
    temp.priceInDollars = 1; 
    inventory.push_back(temp); 

    temp.name = "name2"; 
    temp.quantity = 2; 
    temp.priceInDollars = 2; 
    inventory.push_back(temp); 

    cout << "print:\n"; 
    for each(T_item item in inventory) 
    { 
     cout << item.name << endl; 
     cout << item.quantity << endl; 
     cout << item.priceInDollars << endl << endl; 
    } 

    //modify item at index 1: 
    inventory[1].name = "modified"; 

    //delete item at index 0: 
    inventory.erase(inventory.begin() + 0); 

    cout << "print again:\n"; 
    for each(T_item item in inventory) 
    { 
     cout << item.name << endl; 
     cout << item.quantity << endl; 
     cout << item.priceInDollars << endl << endl; 
    } 
} 

int main() 
{ 
    test(); 
    return 0; 
} 
+0

正如你可以在我編輯的文章中看到的,類Item只有一個虛擬析構函數。我調整了代碼,但似乎我錯過了一些東西,因爲構建時仍然出現錯誤,儘管我沒有編譯錯誤......我認爲當我將函數更改爲void時,我需要調整的不僅僅是庫存類沒有參數。我對麼? – mhenkes92

+0

我不明白你的代碼。我認爲你應該嘗試一些簡單的事情。 –

0

沒有指針,我相信你的代碼的轉換是否正確。由於「庫存向量」是一個成員變量,因此它可以被所有其他成員函數訪問,也可以被操縱。這消除了將向量作爲參數傳遞的要求。 現在爲了返回矢量,我建議添加一個新的「GetInventory()」方法,該方法返回矢量或採用引用來傳遞矢量。

希望它有幫助。

+0

感謝您的評論。我編輯了我的帖子,我收到錯誤。你知道爲什麼嗎?這真是令人沮喪,我一直坐在這裏幾天嘗試所有類型的事情沒有成功,但我相信這個問題很簡單... – mhenkes92

+0

我相信你有問題,因爲沒有定義你聲明的功能。首先你寫的代碼不是一個好的C++代碼。每個東西都應該包裝在一個類中,然後通過對象使用。將你的功能包裝在一個班級中。你也需要定義你的功能。如果你不想讓他們在一個類中,那麼在一個頭文件下使它們成爲全局的,並將其包含在你的程序中。目前你的程序本身就是錯誤的。做一個自我檢討,我會建議。 – Spanky

0

感謝WhozCraig,我的問題已經解決:

「庫存的類定義必須來之前的主()以某種方式將它上面的main(),它應該工作。」