我在编译account.h和account.cpp文件时遇到问题。 在编译时,它给出了account :: account的原型错误,并说它与任何类别的'account'都不匹配,我不知道我错过了什么,因为我称之为account :: account。或者我需要在头文件中定义帐户。如果我该怎么办,它将如何携带num和abal? 这是确切的错误
account.cpp:14:1: error: prototype for ‘account::account(int, float)’ does not match any in class ‘account’
account::account(int num, float abal){
^
In file included from account.cpp:12:0:
account.h:15:7: error: candidates are: account::account(const account&)
class account
^
account.h:15:7: error: account::account()
account.cpp:20:17: error: expected constructor, destructor, or type conversion before ‘(’ token
account::deposit(amount){
这是程序文件
//account.cpp
#include <string>
#include <iostream>
using namespace std;
#include "account.h"
account::account(int num, float abal){
acctnum = num;
intbal = abal;
}
//----------------------------------------------------
//Depositing into account
account::deposit(amount){
if (amount < 0)
{
std::cout << endl <<"The deposit you've enter is negative."
<< amount << " on account " << acctnum << endl;
return 0;
}
else{
balance = amount;
}
}
//----------------------------------------------------
//Withdrawing from account
//If withdrawel exceeds balance provide error and leave balance
//Else subtract withdrawel from account and update balance
account::withdraw(amount){
if (amount < balance){
std::cout << "Debit amount exceeded account balance."
<< amount << " on account "<< acctnum << " with balance "
<< balance << endl;
return 0;
}
else if(amount < 0){
std::cout <<"The withdrawel you've enter is defined as negative."
<< amount << " on account "<< acctnum << " with balance "
<< balance << endl;
return 0;
}
else {
balance -= amount;
}
}
//----------------------------------------------------
//Insert intial balance of account
//If no balance included then give error message and set account balance to 0
account::int_balance(float amount){
if (amount >= 0) {
balance = amount;
}
else {
balance = 0;
std::cout << "Error intial balance invalid" << endl;
}
}
//----------------------------------------------------
account::balance(){
return bal;
}
标头文件
//account.h
#ifndef account_h_
#define account_h_
#include <string>
#include <iostream>
using namespace std;
class account
{
public:
//----------------------------------------------------
//account number
int account_num() const {
return acctnum;
}
//----------------------------------------------------
//constructs bank account with inital_balance
double balance() const {
return bal;
}
//----------------------------------------------------
//deposit into account
void deposit(float amount) {
bal += amount;
}
//----------------------------------------------------
//withdrawal from account
void withdraw(float amount) {
amount - bal;
}
private:
//----------------------------------------------------
//account number
int acctnum;
//----------------------------------------------------
//balance
double bal;
};
#endif
答案 0 :(得分:3)
您尝试在.cpp文件中定义的此签名
account::account(int num, float abal)
尚未在头文件的类定义中声明。
您必须使用类似
的内容声明构造函数account(int num, float abal);
在头文件的类定义中。