使用c++在父类和子类中进行继承时出现错误

Inheritance in SuperClass and SubClasses using c++ giving error

本文关键字:继承 错误 c++ 父类 子类 使用      更新时间:2023-10-16

我正在用c++做一个银行项目

当编译时,它给我一个错误

C:QtQt5.3.0ToolsQtCreatorbinFinanceAppaccount.cpp:5: error: redefinition of 'Account::Account(QString, QString, double, QString)' Account::Account(QString cn, QString an, double ir, QString ty) ^

下面是我的文件 超类

account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <QString>
#include <QList>
#include <transaction.h>

class Account
{
public:
    Account(QString cn, QString an, double ir, QString ty) : custName(cn), accNum(an), interestRate(ir), type(ty){}
    virtual ~Account();
    QString getCustName();
    QString getAccNum();
    QList<Transaction> getTransaction();
    virtual QString toString();
    QString getType();
    void setType(QString ty);
    double getInterestRate() const;
    virtual void transaction(double amt) = 0;
    virtual void calcInterest() = 0;
    void getAccountInfo();
protected:
    double balance;
    QList<Transaction> transactions;
private:
    QString custName;
    QString accNum;
    double interestRate;
    QString type;
};
#endif // ACCOUNT_H

account.cpp

#include "account.h"
#include <iostream>
#include <conio.h>
Account::Account(QString cn, QString an, double ir, QString ty)
{
}
QString Account::toString(){
    QString accountString;
    accountString += "ACCOUNT DETAILS";
    accountString += "---------------";
    accountString += "ntAccount No    : " + accNum;
    accountString += "ntAccount holder: " + custName;
    accountString += "ntBalance       : " + QString::number(balance,'f',2);
    accountString += "nntTransactions";
    accountString += "nnt------------";
    foreach(Transaction single_transact, this->transactions)
    {
        accountString += "ntt" + single_transact.toString();
    }
    return accountString;
}
QString Account::getCustName(){
    return this->custName;
}
QString Account::getAccNum(){
    return this->accNum;
}
QList<Transaction> Account::getTransaction(){
    return this->transactions;
}
QString Account::getType(){
    return this->type;
}
void Account::setType(QString ty){
    this->type = ty;
}
double Account::getInterestRate()const{
    return this->interestRate;
}
void Account::getAccountInfo(){
    std::string cn, an, ty;

        std::cout<< "nnEnter Customer Name :- ";
        std::cin>> cn;
        std::cout<< "Enter Account Number :- ";
        std::cin>> an;
        std::cout<< "Enter Account Type :- ";
        std::cin>> ty;
        this->custName = QString::fromStdString(cn);
        this->accNum = QString::fromStdString(an);
        this->type = QString::fromStdString(ty);
}
<标题> 子类

savingsaccount.h

#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include <QString>
#include "account.h"
class SavingsAccount : public Account
{
public:
    SavingsAccount();
    SavingsAccount(QString cn, QString an, double ir, QString ty);
    double minIntEarnedForPoints;
    void transaction(double amt);
    void calcInterest();
    int getPoints();
    QString toString();
    void make_withdrawal();
    void make_deposit();
    void print_statement();
    void print_balance();
    void print_account_type();
    void print_points_earned();
private:
    int points;
};
#endif // SAVINGSACCOUNT_H

savingsaccount.cpp

#include "savingsaccount.h"
#include <QString>
#include <QList>
#include <iostream>
#include <conio.h>
SavingsAccount::SavingsAccount(QString cn, QString an, double ir, QString ty):{
    this->custName = cn;
    this->accNum = an;
    this->interestRate = ir;
    this->type = ty;
}
void SavingsAccount::transaction(double amt){
    QString transaction_type;
    this->setType("Savings");
    if(amt < 0){
        transaction_type = "Withdrawal";
    }else{
        transaction_type = "Deposit";
    }
    if(this->balance >= amt){
        this->balance += amt;
        Transaction _t = Transaction(transaction_type, amt);
        this->transactions::append(_t);
        std::cout << "Transaction successful.n";
        std::cout << this->toString() + "n" + transaction_type + "t:t" + amt;
    }else{
        std::cout << "Insufficient funds.";
    }
}
void SavingsAccount::calcInterest(){
    QString transaction_type = "Interest";
    double new_interest = this->balance * (this->getInterestRate()/100) * (1/12);
    this->balance += new_interest;
    int points_earned = 0;
    if(new_interest > this->minIntEarnedForPoints){
        points_earned = (int)new_interest;
    }
    this->points += points_earned;
    Account::transactions.append(new Transaction(transaction_type, new_interest));
    std::cout << "Transaction successful.n";
    std::cout << this->toString() + "n" + "Interest" + "t:t" + new_interest;
    std::cout << "nPoints t:t"+QString::number(points_earned);
}
void SavingsAccount::print_points_earned(){
    std::cout << "Points Earned: " << this->points;
}
void SavingsAccount::getPoints(){
    return this->points;
}
QString SavingsAccount::toString(){
    QString savings_account = "SAVINGS ACCOUNTn";
    savings_account += "------------------------------";
    savings_account += Account::toString();
    savings_account += "nMinimum Earned Points: " + QString::number(this->getPoints(),'f',2);
    savings_account += "nn";
    return savings_account;
}
void SavingsAccount::make_withdrawal(){
    double amt;
    std::cout <<"nEnter amount to Withdraw :- ";
    std::cin>>amt;
    double amount = 0-amt;
    transaction(amount);
}
void SavingsAccount::make_deposit(){
    double amt;
    std::cout <<"nEnter amount to Deposit :- ";
    std::cin>>amt;
    transaction(amt);
}
void SavingsAccount::print_statement(){
    std::cout << "Statement";
    std::cout << "---------";
    foreach(Transaction single_transact, this->transactions)
    {
        std::cout << single_transact.toString();
    }
}
void SavingsAccount::print_balance(){
    std::cout << "ChequeAccount Balance: " << this->balance;
}
void SavingsAccount::print_account_type(){
    std::cout << this->toString();
}

我需要帮助如何正确地使用超类构造子类我被上面突出显示的错误卡住了。它还显示了另一个错误

C:QtQt5.3.0ToolsQtCreatorbinFinanceAppaccount.h:11: error: 'Account::Account(QString, QString, double, QString)' previously defined here Account(QString cn, QString an, double ir, QString ty) : custName(cn), accNum(an), interestRate(ir), type(ty){} ^

非常感谢任何帮助。非常感谢

您在头文件中定义了Account::Account构造函数。

然后在account.cpp中再次定义它,其中包括头文件。这是重复的定义。

要么只在头文件中声明构造函数;或者将定义留在那里,并从account.cpp中删除。