我正在尝试使用清单类来计算清单物品的总成本,我收到了一些错误消息,但我不知道为什么,这些错误是我的imp文件中未声明的标识符...这是什么我到目前为止。

库存

#include <string>
#include <iostream>
using namespace std;


class Inventory
{
public:
    void print() const;
    void setItemNumber(int num);
    void setQuantity(int qty);
    void setCost(double cst);
    void setTotalCost(double total);
    int getItemNumber() const;
    int getQuantity() const;
    double getCost() const;
    double getTotalCost () const;

    Inventory(int num = 0, int qty = 0, double cst = 0, double total = 0);
    Inventory(int num, int qty, double cst, double total);


private:
    int itemNumber;
    int quantity;
    double cost;
    double totalCost;


};


InventoryImp.cpp

#include <iostream>
#include "InventoryClass.h"

using namespace std;


void Inventory::print() const
{
    if (itemNumber > 0 && quantity > 0 && cost > 0)
        cout << itemNumber << quantity << cost
                << totalCost;
}

void Inventory::setCost(double cst)
{
    cost = cst;
}

void Inventory::setItemNumber(int num)
{
    itemNumber = num;
}

void Inventory::setQuantity(int qty)
{
    quantity = qty;
}

void Inventory::setTotalCost(double total)
{
    totalCost = total;
}

double Inventory::getCost() const
{
    return cst;
}

int Inventory::getItemNumber() const
{
    return num;
}

int Inventory::getQuantity() const
{
    return qty;
}

double Inventory::getTotalCost() const
{
    return qty * cst;
}


Inventory::Inventory(int num, int qty, double cst, double total)
{
    cost = cst;
    quantity = qty;
    totalCost = total;
    itemNumber = num;
}
Inventory::Inventory(int num, int qty, double cst, double total)
{
    cost = cst;
    quantity = qty;
    totalCost = total;
    itemNumber = num;
}


Main.cpp

#include <iostream>
#include "InventoryClass.h"
using namespace std;

int main()
{
    Inventory Item1(101, 6, 3.00);
    Inventory Item2(102, 1, 1.00);
    Inventory Item3(103, 8, 7.00);
    Inventory Item4(104, 4, 12.00);
    Inventory Item5(105, 6, 5.00);
    Inventory Item6(106, 3, 9.00);

    Item1.print();
    cout << endl;
    Item2.print();
    cout << endl;
    Item3.print();
    cout << endl;
    Item4.print();
    cout << endl;
    Item5.print();
    cout << endl;
    Item6.print();
    cout << endl;

    return 0;
}

最佳答案

除了forsvarir解决的主要问题之外,您的其他错误似乎围绕着未定义的变量。您实际上定义了它们,但在几个地方拼错了它们。

开始训练自己使用一些区分成员变量,全局变量,参数等的约定(只要保持一致就没关系)。例如,命名成员变量“ _member”或“ mMember”或其他名称。这将节省您自己和其他人的痛苦。

09-10 04:03
查看更多