未定义的函数引用 "CashRegister::GetTotalPrice"

Undefined reference to function "CashRegister::GetTotalPrice"

我正在尝试在单独的 class 中调用几个函数,就像这样;

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

如果您需要,我可以 post 其余的,但它很多。如果有任何帮助,我将不胜感激!

编辑: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;
}

你的header:

double GetTotalPrice(InventoryItem myItem, int numPurchased)

你的.cpp

double GetTotalPrice(InventoryItem myItem, double numPurchased)

.cpp 文件中的函数签名与 header 文件中的函数签名不匹配。 .cpp 文件中的函数也是一个自由函数,不是 class 的一部分。

您的.cpp需要这个:

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

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