账户银行行使 - 在类之外声明“x”不是定义

时间:2015-08-25 19:57:07

标签: c++ class

我无法理解什么是错的。它可能是微不足道的但我只是一个乞丐,所以请帮助我,我不知道该怎么做

此代码用于练习“如何编写Deitel第8版

”一书
// class definition

include<iostream>

class Account
{
public:
    Account ( int  );
    void credit( int );
    void debit( int );
    int getBalance();
private:
    int currentBalance;
};
#include <iostream>
#include "Account.h"
using namespace std;

int main(){

cout << "Enter the initial balance of two accounts: ";
int money;
cin >> money;

Account constructor1( money );

cout << "What would you like to do? \n\n"
    << "Type 1 for crediting, 2 for debiting";
int choose;
cin >> choose;

if( (choose != 1) || (choose != 2) )
     cout << "Invalid option.";
else if( choose = 1 )
    {
    cout << "Your current balance is: " << constructor1.getBalance() <<      "/n";
    cout << "What amount of money would you like to enter ? ";
    int cred;
    cin >> cred;
    constructor1.credit(cred);
    cout << "Your current balance is: " << constructor1.getBalance();
    }
 else
    {
    cout << "Your current balance is: " << constructor1.getBalance() <<     "/n";
    cout << "What amount of money would you like to withdraw ? ";
    int deb;
    cin >> deb;
    constructor1.debit(deb);
    cout << "Your current balance is: " << constructor1.getBalance();
    }
 }
// member functions definitions
#include "Account.h"
#include <iostream>
using namespace std;

Account::Account( int initBalance )
{
    if(initBalance >= 0)
        currentBalance = initBalance;
    else
    {
        currentBalance = 0;
        cout << "The initial value is invalid. ";
    }
}
void Account::credit(int amount)
{
    currentBalance += amount;
}

void Account::debit(int amount)
{
    if( amount > currentBalance)
    {
        cout << "Debit amount excceed account balance.";
    }
    else
    {
        currentBalance -= amount;
    }
}

int Account::getBalance();
{
    return currentBalance;
}

1 个答案:

答案 0 :(得分:1)

这是一个错字:

int Account::getBalance();
{
    return currentBalance;
}

您在;之后放置(),不应该在那里

int Account::getBalance()
{
return currentBalance;
}

最后:else if( choose = 1 )应为else if( choose == 1 ),因为等于运算符为==而非=(即赋值运算符)