对函数“ CashRegister :: GetTotalPrice”的未定义引用

时间:2019-11-06 22:20:41

标签: c++ codeblocks

我试图像这样在一个单独的类中调用一些函数;

CashRegister* reg = new CashRegister();
 Subtotal = reg->GetTotalPrice(Shelf[Choice], Quantity);
            Tax = reg->GetTax(Subtotal);
            totalCost = Subtotal + Tax;
            cout << endl << "Aaah, big spender I see. That's going to cost you..." << endl;
            cout << "Subtotal: " << totalCost << endl << "Sales Tax: " << Tax << endl << "Total" << totalCost << endl;

但是我收到错误消息“对CashRegister::GetTotalPrice(InventoryItem, int)的未定义引用以及其他调用的相同错误。

这是CashRegister.h

    #ifndef CASHREGISTER_H_INCLUDED
    #define CASHREGISTER_H_INCLUDED
    #include <string>
    #include "InventoryItem.h"
    using namespace std;

    //Constant variables
        const int PROFIT = .3;
        const int SALES_TAX = .06;
    class CashRegister
    {
        public:
        CashRegister();

        ~CashRegister();

        double GetTotalPrice(InventoryItem myItem, int numPurchased); //Gets the total price and returns it (INC. profit).
        double GetTax(double price); //Calculates sales tax and returns it (JUST the tax)
        double GetProfit(double price); //Adds the profit margin to the item and returns the value
    };


#endif // CASHREGISTER_H_INCLUDED

如果需要的话,我可以发布其余的,但是很多。非常感谢您的帮助!

编辑:CashRegister.cpp;

#include "CashRegister.h"

CashRegister::CashRegister()
{

}

// Destructor
CashRegister::~CashRegister()
{

}
double GetProfit(double price)
{
    price = price * PROFIT;
    return price;
}

double GetTax(double price)
{
    price = price * SALES_TAX;
    return price;
}

double GetTotalPrice(InventoryItem myItem, double numPurchased)
{
    double myPrice = myItem.getCost();
    myPrice = myPrice * numPurchased;
    myPrice = GetProfit(myPrice);
    return myPrice;
}

1 个答案:

答案 0 :(得分:2)

您的标题:

double GetTotalPrice(InventoryItem myItem, int numPurchased)

您的.cpp

double GetTotalPrice(InventoryItem myItem, double numPurchased)

.cpp文件中的函数签名与头文件中的函数签名不匹配。 .cpp文件中的函数也是免费函数,不是类的一部分。

您需要在.cpp中使用它:

double CashRegister::GetTotalPrice(InventoryItem myItem, int numPurchased) {
//...
}

在头文件中声明的其他成员函数也是如此。在CashRegister::文件中的函数名称之前添加.cpp