我正在尝试使用库存类计算库存项目的总成本,我收到了一些错误消息,我无法弄清楚为什么,这些错误是我的 ..imp 文件中未声明的标识符. 这是我目前所拥有的。
库存.h
#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;
}
最佳答案
首先,您只需要该函数的一个实例...
Inventory(int num = 0, int qty = 0, double cst = 0, double total = 0);
第二个参数相同且没有默认值是多余的,编译器将无法区分它们。
关于c++ - 存货类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5805099/