2015-05-27 169 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(),它应该工作。”