【发布时间】:2016-02-15 23:40:36
【问题描述】:
我的程序文件有问题。我已经在我的头文件中声明了“存款”,但仍然期待程序文件中有一些东西。我是否需要定义金额和我错过了什么?谢谢
account.cpp:22:17: error: expected constructor, destructor, or type conversion before ‘(’ token
account::deposit(amount) {
^
Program.cpp
#include <string>
#include <iostream>
using namespace std;
#include "account.h"
//----------------------------------------------------
//Account details
int account::acct( int num, int int_balance){
acctnum = num;
bal = int_balance;
}
//----------------------------------------------------
//Depositing into account
account::deposit(amount) {
if (amount < 0) {
std::cout << "The deposit you've enter is negative."
<< amount << " on account " << acctnum << endl;
}
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;
}
else if(amount < 0) {
std::cout <<"The withdrawel you've enter is defined as negative."
<< amount << " on account "<< acctnum << " with balance "
<< balance << endl;
}
else {
balance -= amount;
}
}
//----------------------------------------------------
//Insert intial balance of account
//If no balance included then give error message and set account balance to 0
int_balance(amount){
if (amount >= 0) {
balance = amount;
}
else {
balance = 0;
std::cout << "Error intial balance invalid" << endl;
}
}
//----------------------------------------------------
balance(){
return bal;
}
account.h 标头
#ifndef account_h_
#define account_h_
#include <string>
#include <iostream>
using namespace std;
class account
{
public:
//----------------------------------------------------
//account
int acct(int num, int int_balance);
//----------------------------------------------------
//account number
int account_num() const {
return acctnum;
}
//----------------------------------------------------
//constructs bank account with inital_balance
double balance() const {
return bal;
}
//----------------------------------------------------
//withdrawal from account
void withdraw(float amount) {
amount - bal;
}
//----------------------------------------------------
//deposit into account
void deposit(float amount) {
amount + bal;
}
private:
//----------------------------------------------------
//account number
int acctnum;
//----------------------------------------------------
//balance
double bal;
};
#endif
【问题讨论】:
-
定义时需要指定
account::deposit的返回类型。应该是:void account::deposit(amount) { ... }
标签: c++ constructor compiler-errors