C++类向量问题

C++ class vector issues

本文关键字:问题 向量 C++      更新时间:2023-10-16

好的,我正在开发这个库存程序,在这个程序中,应该发生的是用户输入关于特定产品的各种信息,然后将其传递给库存,同时让用户有机会更改东西(正如您在代码中看到的)。由于我这样做是为了一个学校项目,我必须有各种各样的课程(因此我没有摆脱它们)。

现在我的问题是库存类中的向量。

错误"std::vector>AddProduceToInventory(std::vector<_Ty,std::allocater<_Ty>>)":无法将参数1从"std:vector>"转换为"std::vector>"

这个问题适用于类中的每个向量。我只复制了一个相同的错误。

我不知道还能做什么。这是我的代码(抱歉太长了!)

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Item {
public:
    void SetPrice(int prcInDllrs) {
        priceInDollars = prcInDllrs; // Somehow I need to keep adding each of these totals to make a grand total price.
    }
    void SetItemPrice(int whatever) {
        itemsPrice = whatever;
    }
    void SetName(string nm)
    {
        name = nm;
    };
    void SetQuantity(int qnty)
    {
        quantity = qnty;
    };
    virtual void Print()                                    //Here is my print problem
    {
    };
    virtual ~Item()
    {
        return;
    };
protected:
    string name;
    int    quantity = 0;
    int priceInDollars = 0;
    int itemsPrice = 0;
};
class Produce : public Item { // Derived from Item class
public:
    void SetExpiration(string expir)
    {
        expiration = expir;
    };
    void Print()
    {
        cout << name << " x" << quantity
            << " (Expires: " << expiration << ")" << " for $" << itemsPrice //FILL THIS IN!!!
            << endl;
    };
private:
    string expiration;
};
class Book : public Item {
public:
    void SetAuthor(string athr) {
        author = athr;
    }
    void Print()
    {
        cout << name << " x" << quantity
            << " for $" << itemsPrice << " (Author: " << author << ")" << endl;
    }
private:
    string author;
};

class Inventory {
public:
    void PrintInventory()
    {
        if (inventory.size() == 0) {
            cout << "No items to print." << endl;
        }
        else {
            for (unsigned int i = 0; i < inventory.size(); ++i) {
                cout << i << " - ";
                inventory.at(i)->Print();
            }
        }
        cout << "Total inventory value: $" << totalInvPriceInDollars << endl;
    }
    //I am leaving these out of the equation until I get the PrintInventory working
    void AddItemToInventory()
    {
        vector<Inventory*> AddProduceToInventory(vector<Inventory*> Invntry);
        AddProduceToInventory(vector<Inventory*> inventory)
        {
            Produce* prdc;
            string usrInptName = "";
            string usrInptQntyStr = "";
            istringstream inSS;
            int usrInptQnty = 0;
            string usrInptExpr = "";
            string usrInptPrcStr = "";
            int usrInptPrc = 0;
            int itemsTotalPrice = 0;
            istringstream inDD;
            string runningTotal = "";
            int sum = 0;
            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: $";
            getline(cin, usrInptPrcStr);
            inDD.str(usrInptPrcStr);
            inDD >> usrInptPrc;
            inDD.clear();
            itemsTotalPrice = usrInptPrc * usrInptQnty;
            prdc = new Produce;
            prdc->SetName(usrInptName);
            prdc->SetQuantity(usrInptQnty);
            prdc->SetExpiration(usrInptExpr);
            prdc->SetPrice(usrInptPrc);
            prdc->SetItemPrice(usrInptPrc);
            inventory.push_back(prdc);
            Inventory set;
            set.SetInventory(inventory);
            return inventory;
        }
    }
    void AddBookToInventory()
    {
        vector<Inventory*> AddBookToInventory(vector<Inventory*> inventory) {
            Book* prdct;
            string usrInptName = "";
            string usrInptQntyStr = "";
            istringstream inSS;
            int usrInptQnty = 0;
            string usrInptAthr = "";
            string usrInptPrcStr = "";
            int usrInptPrc = 0;
            int itemsTotalPrice = 0;
            istringstream inDD;
            string runningTotal = "";
            int sum = 0;
            cout << "Enter name of new book: ";
            getline(cin, usrInptName);
            cout << "Enter quantity: ";
            getline(cin, usrInptQntyStr);
            inSS.str(usrInptQntyStr);
            inSS >> usrInptQnty;
            inSS.clear();
            cout << "Enter author: ";
            getline(cin, usrInptAthr);
            cout << "Enter the price per item: $";
            getline(cin, usrInptPrcStr);
            inDD.str(usrInptPrcStr);
            inDD >> usrInptPrc;
            inDD.clear();
            prdct = new Book;
            prdct->SetName(usrInptName);
            prdct->SetQuantity(usrInptQnty);
            prdct->SetPrice(usrInptPrc);
            prdct->SetAuthor(usrInptAthr);
            prdct->SetItemPrice(usrInptPrc);
            inventory.push_back(prdct);
            Inventory set;
            set.SetInventory(inventory);
            return inventory;
        }
    }
    void UpdateItemQtyInventory()
    {
        //This is the update function in which we can change how many items a certain purchase has
        vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory) {
        string usrIndexChoiceStr = "";
        unsigned int usrIndexChoice = 0;
        istringstream inSS;
        string usrInpuQntyStr = "";
        int usrInptQnty = 0;

        if (inventory.size() == 0) {
            cout << "No items to update." << endl;
        }
        else {
            PrintInventory(inventory);
        do {
            cout << "Update which item #: ";
            getline(cin, usrIndexChoiceStr);
            inSS.str(usrIndexChoiceStr);
            inSS >> usrIndexChoice;
            inSS.clear();
        } while (!(usrIndexChoice < inventory.size()));
        cout << "Enter new quantity: ";
        std::getline(cin, usrInptQntyStr);
        inSS.str(usrInptQntyStr);
        inSS >> usrInptQnty;
        inSS.clear();
        inventory.at(usrIndexChoice)->SetQuantity(usrInptQnty);
        }
        return inventory;
        }
    }
    void RemoveItemFromInventory()
    {
        //Here we will be removing an entire item from the inventory
        vector<Item*> RemoveItemFromInventory(vector<Item*> inventory) {
        istringstream inSS;
        string usrIndexChoiceStr = "";
        unsigned int usrIndexChoice = 0;
        string usrInptQntyStr = "";
        if (inventory.size() == 0) {
            cout << "No items to remove." << endl;
        }
        else {
            PrintInventory(inventory);
        do {
            cout << "Remove which item #: ";
            getline(cin, usrIndexChoiceStr);
            inSS.str(usrIndexChoiceStr);
            inSS >> usrIndexChoice;
            inSS.clear();
        } while (!(usrIndexChoice < inventory.size()));
        inventory.erase(inventory.begin() + usrIndexChoice);
        }
        return inventory;
        }
    }
    void GetTotalValueAsPrice()
    {
    }
    void SetInventory(vector<Item*> invntry) {
        inventory = invntry;
    }
private:
    int totalInvPriceInDollars = 0;
    vector<Inventory*> inventory;
};
// 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 create a new book, then add that book to the inventory
vector<Item*> AddBookToInventory(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<Inventory*> inventory1;
    string usrInptOptn = "default";
    string usrInptOptn2 = "default";
    Inventory update;
    update.UpdateItemQtyInventory();
    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') {
            Inventory printer;
            printer.PrintInventory();
            update.SetInventory(inventory1);
        }
        else if (usrInptOptn.at(0) == 'a') {
            cout << "nEnter (b)ook or (p)roduce: ";
            getline(cin, usrInptOptn2);
            if (usrInptOptn2.at(0) == 'b') {
                inventory1 = AddBookToInventory(inventory1);
                inventoryUpdate.SetInventory(inventory1);
            }
            else if (usrInptOptn2.at(0) == 'p') {
                inventory1 = AddItemToInventory(inventory1);
                inventoryUpdate.SetInventory(inventory1);
            }
            else
            {
                continue;
            }
        }
        else if (usrInptOptn.at(0) == 'u') {
            inventory1 = UpdateItemQtyInInventory(inventory1);
        }
        else if (usrInptOptn.at(0) == 'r') {
            inventory1 = RemoveItemFromInventory(inventory1);
        }
        else if (usrInptOptn.at(0) == 'q') {
            cout << "nGood bye." << endl;
            break;
        }
    }
    return 0;
}

我也收到了额外的错误,但我认为它们来自这个主要问题。我该怎么办才能解决这个问题?

谢谢!此外,我正在使用Visual Studio,但已经检查了Code::Blocks,以确保这不是一个奇怪的编译器问题。

您的大多数函数看起来都类似于以下内容:

void foo() {
    std::vector<T> foo(std::vector<T> bar){
        /*...*/
    }
}

看起来你试图在函数内部声明一个函数(具有相同的名称,但不同的参数和返回类型,有时还缺少返回类型)。只是不要那样做,而只是这样:

std::vector<T> foo(std::vector<T> bar){
    /*...*/
}