c++ - 使用 C++ 的父类(super class)和子类中的继承给出错误

标签 c++ inheritance casting subclass superclass

我正在用 C++ 开发一个银行项目

编译时报错

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\FinanceApp\account.cpp:5: 错误:'Account::Account(QString, QString, double, QString)' 的重新定义 账户::账户(QString cn, QString an, double ir, QString ty) ^

下面是我的文件

父类(super class)

账户.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

账户.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 += "\n\tAccount No    : " + accNum;
    accountString += "\n\tAccount holder: " + custName;
    accountString += "\n\tBalance       : " + QString::number(balance,'f',2);
    accountString += "\n\n\tTransactions";
    accountString += "\n\n\t------------";

    foreach(Transaction single_transact, this->transactions)
    {
        accountString += "\n\t\t" + 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<< "\n\nEnter 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);

}

子类

储蓄账户.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

储蓄账户.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 ACCOUNT\n";
    savings_account += "------------------------------";
    savings_account += Account::toString();
    savings_account += "\nMinimum Earned Points: " + QString::number(this->getPoints(),'f',2);
    savings_account += "\n\n";
    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();
}

我需要有关如何使用父类(super class)正确构造子类的帮助 我坚持上面突出显示的错误。它还显示另一个错误

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\FinanceApp\account.h:11: error: 'Account::Account(QString, QString, double, QString)' 之前在这里定义 账户(QString cn, QString an, double ir, QString ty) : custName(cn), accNum(an), interestRate(ir), type(ty){} ^

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

最佳答案

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

然后您在包含头文件的account.cpp 中再次定义它。那是你的重复定义。

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

关于c++ - 使用 C++ 的父类(super class)和子类中的继承给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39421559/

相关文章:

c++ - 我应该如何将对象传递给类以检查碰撞?

sql - 在 PSQL 中将字符串转换为十进制

sql - Postgres 整数类型的下限超出范围?

ios - 覆盖 UITableViewController 子类中的 cellForRowAtIndexPath 返回错误 : does not override any method from superclass

c++ - 纯虚函数中的默认值参数

C 保证重新 : Pointers to Void and Character Types, 类型转换

c++ - 如何创建模板类型迭代器的 STL 对象?

c++ - C++中的任意类型转换

c++ - 不是 lambda 函数中的常量表达式

java - 继承同名但返回类型不同的方法