C ++类向量问题

时间:2015-11-20 18:03:14

标签: c++ vector

好的,我正在研究这个库存计划,其中应该发生的是用户输入有关特定产品的各种信息,然后将其传递到库存,同时为用户提供机会改变东西(你应该在代码中看到)。因为我为一个学校项目做这个,所以我必须有各种各样的课程(因此我没有摆脱它们。

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

错误' std :: vector> AddProduceToInventory(std :: vector< _Ty,std :: allocator< _Ty>>)':无法从' std :: vector>'转换参数1到' 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进行检查,以确保它不是一个奇怪的编译器问题。

1 个答案:

答案 0 :(得分:1)

你的大部分功能看起来都与此类似:

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

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

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