c ++错误:具有声明的头文件的预期构造函数

时间:2016-02-15 23:40:37

标签: c++ constructor compiler-errors

我的程序文件存在问题。我在我的头文件中声明了“存款”,但仍然期待程序文件中的某些内容。我需要定义金额吗?我缺少什么?感谢

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

1 个答案:

答案 0 :(得分:0)

实施有一种金额类型。

 void deposit(float amount) {
     amount + bal;
 }

对于多种方法,您似乎已在.cc文件中忘记了它。参数需要一个类型。