我是新来的,我正在寻找用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);
接下来是main和我的类Inventory,它现在有四个转换函数,没有返回,没有参数,这些参数最初不在类Inventory中,并且之前有参数/返回。
以下是整个代码,我认为理解这种方式会更好:
#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 :(得分: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;
}
答案 1 :(得分:0)
我相信您的代码转换是正确的。由于'库存矢量'是一个成员变量,它可以被所有其他成员函数访问,也可以被操作。这消除了将vector作为参数传递的要求。 现在为了返回向量,我建议添加一个新的“GetInventory()”方法,该方法返回向量或者引用传递向量。
希望它有所帮助。
答案 2 :(得分:0)
感谢WhozCraig,我的问题已经解决了:
&#34; Inventory的类定义必须以某种方式出现在main()之前。将它移到main()之上,它应该可以工作。&#34;