为什么我得到对Class::Class()的未定义引用

Why am I getting undefined reference to Class::Class()?

本文关键字:Class 未定义 引用 为什么      更新时间:2023-10-16

error: undefined reference to ' Transaction::Transaction(QString, QDate, int, double)'

从头文件中提取:

#include <Transaction.h>
class Product
{
public:
    virtual ~Product();
    void sell(int n);
    void restock(int n);
    void setProductCode(QString c);
    QString getSupplierCode() const;
    QString getProductCode() const;
    QList<Transaction> getTransactions();
    QString toString();
    void removeAll();
    bool isExpired() const;
protected:
    Product(QString name, int num, double seprice, double suprice, QString sc);
    Product(QStringList& prodlist);
private:
    QString m_Name;
    int m_NoOfItems;
    QString m_ProductCode;
    double m_SellingPrice;
    double m_SupplierPrice;
    QString m_SupplierCode;
    QList<Transaction> m_Transaction;
};
实现文件:

//Sell a product
void Product::sell(int n)
{
    if(m_NoOfItems == 0)
  {
    qDebug() << "Out of stock";
  }
  else if(n < m_NoOfItems)
    {
        m_NoOfItems = m_NoOfItems -n;
        m_Transaction.append(Transaction("Sale", QDate::currentDate(),n, m_SellingPrice));
    }
    else qDebug() << "Not enough items in stock";
}
//Restock a product
void Product::restock(int n)
{
    m_NoOfItems = m_NoOfItems +n;
    m_Transaction.append(Transaction("Purchase", QDate::currentDate(),n, `m_SupplierPrice));`
}

Transaction.h

#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <QString>
#include <QDate>
//begining of Transaction Class
class Transaction
{
public:
  Transaction(QString type, QDate date, int num, double price );
  QString toString() const;
private:
  QString m_Type;
  QDate m_Date;
  int m_NoOfItems;
  double m_PricePerItem;
};
//end of Transaction class
#endif // TRANSACTION_H

我得到对Transaction::Transaction(QString, QDate, int, double)的未定义引用。应该如上所述。我写class::class是因为我已经提到过这个类了。

事务类不可见。我在同一文件的Product类下面定义了它。我必须把它放在一个单独的头文件中