1

好的,所以我在使用这个我应该为学校做的 C++ 程序时遇到了问题,我需要一些帮助来解决我不断遇到的错误。我有基础知识,但我需要关于类和对象的帮助。

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class BankAccount {
private:
    double accountBalance;
    string name;        
public:
    BankAccount();
    BankAccount(string,double);
    double deposit(double);
    double withdraw(double);
    void checkBalance();                    
};

BankAccount::BankAccount()
{
    accountBalance=0;
    name="";
}

BankAccount::BankAccount(string name,double money)
{
    name=name;
    accountBalance=accountBalance;                                
}

double BankAccount::deposit(double money)
{
    accountBalance+=money;
    return accountBalance;
}

double BankAccount::withdraw(double money)
{
    accountBalance-=money;
    return accountBalance;
};            

void BankAccount::checkBalance()
{
    cout<<"The balance on the account is $"<<accountBalance<<"!!"<<endl;     
};                                                                                                             
int main(int argc, char *argv[])
{
    int c;
    double m;
    string n;
    cout<<"==================Bank======="<<endl;
    cout<<"[1] Open a new Bank Account |"<<endl;
    cout<<"[2] Deposit money           |"<<endl;
    cout<<"[3] Withdraw money          |"<<endl;
    cout<<"[4] Check balance           |"<<endl;
    cout<<"============================="<<endl;
    cout<<endl;
    cout<<"What would you like to do :";
    cin>>c;

    switch (c){
    case 1:
        cout<<"Ok I see you want to open a new Bank Account"<<endl;
        cout<<"But first answer a few questions:"<<endl;
        cout<<"What is your name? ";
        cin>>n;
        cout<<"Next tell me the amount of money you wish to open your account with: ";
        cin>>m;
        BankAccount::BankAccount(n,m);
        cout<<"OK all set, "<<n<<"!!"<<endl;
        break;
    case 2:
        cout<<"How much money would you like to deposit? : ";
        cin>>m;
        BankAccount.deposit(m);
        break;
    case 3:
        cout<<"How much money would you like to withdraw? : ";
        cin>>m;
        BankAccount.withdraw(m);
        break;
    case 4:
        cout<<"OK I'll check your balance"<<endl;
        BankAccount.checkBalance();               
        break;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

因此,如果您能帮助我,将不胜感激。

4

2 回答 2

3

BankAccount是类型名称,而不是变量名称。您不能在类型上调用实例方法。

创建一个类型为 的变量BankAccount,为其分配一个实例,然后使用您已有的相同符号调用实例上的方法:

BankAccount acct;
switch (c){
case 1:
     cout<<"Ok I see you want to open a new Bank Account"<<endl;
     cout<<"But first answer a few questions:"<<endl;
     cout<<"What is your name? ";
     cin>>n;
     cout<<"Next tell me the amount of money you wish to open your account with: ";
     cin>>m;
     acct = BankAccount(n,m);
     cout<<"OK all set, "<<n<<"!!"<<endl;
     break;
case 2:
     cout<<"How much money would you like to deposit? : ";
     cin>>m;
     acct.deposit(m);
     break;
case 3:
     cout<<"How much money would you like to withdraw? : ";
     cin>>m;
     acct.withdraw(m);
     break;
case 4:
     cout<<"OK I'll check your balance"<<endl;
     acct.checkBalance();               
     break;
}
于 2013-04-11T20:49:33.223 回答
0

BankAccount是您定义的类,您不能使用::运算符访问non-static该类的成员。在这种情况下,您需要首先创建 的对象BankAccount,然后使用dot operator来访问deposit和其他成员函数,因为这些non-static成员函数与类的实例相关联。

如果deposit和其他相关函数是static成员函数,则可以这样做。

于 2013-04-11T20:52:43.693 回答