使用c++在父类和子类中进行继承时出现错误
Inheritance in SuperClass and SubClasses using c++ giving error
我正在用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
中删除。
相关文章:
- 多级继承错误 C2280:"atttempting to reference a deleted function"
- 谷神星求解器成本函数继承错误:模板可能不是虚拟的
- C 预期的类名称{token,一个继承错误
- C++ 继承:错误:候选人需要 1 个参数,提供 0
- 继承错误:'{'标记之前的预期类名
- C++类多重继承错误
- 用模板类中的C 继承错误
- 在堆栈上分配对象时发生C++继承错误
- C++嵌套继承错误"expected class-name before ‘{’ token"
- 继承:错误:聚合类型A的类型不完整,无法定义
- 多重继承错误'invalid use of incomplete type'
- 继承错误 - 无法实例化抽象类 无法访问在类中声明的受保护成员
- C++ 继承错误:不明确的错误
- 类继承错误:私有成员
- 继承错误
- C++构造函数继承错误
- c++中的继承:错误C2011
- 多级继承错误"cout does not name a type"
- c++中模板继承错误
- c++类继承错误