我正在尝试使用库存类计算库存物品的总成本,我收到一些错误消息,我无法找出原因,错误是我的imp文件中未声明的标识符..这是我到目前为止所拥有的。
Inventory.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;
}
答案 0 :(得分:4)
首先,您只需要此功能的一个实例...
Inventory(int num = 0, int qty = 0, double cst = 0, double total = 0);
第二个,具有相同的参数且没有默认值是多余的,编译器无法区分它们。
答案 1 :(得分:3)
除了forsvarir解决的主要问题之外,您的其他错误似乎围绕未定义的变量。你实际上定义了它们,但在几个地方拼错它们。
开始训练自己使用一些约定(只要你保持一致无关紧要),区分成员变量,全局变量,参数等。例如命名成员变量“_member”或“mMember”管他呢。它将拯救你自己和其他人的悲伤。