错误:''之前的预期主表达式。令牌 [英] Error : expected primary-expression before '.' token

查看:1349
本文介绍了错误:''之前的预期主表达式。令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有麻烦这个C ++程序我应该为学校做,我需要一些帮助,一个错误,我不断得到。我有基础,但我需要帮助类和对象。

Ok so I'm having trouble with this C++ program I am supposed to do for school and I need some help with an error I keep getting. I have the basics but I need help with classes and objects.

#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;
}

所以如果你能帮助我,

So if you could help me that would be very much appreciated.

推荐答案

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

BankAccount is a type name, not a variable name. You cannot invoke instance methods on a type.

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

Create a variable of type BankAccount, assign it an instance, and then call methods on the instance using the same notation that you already have:

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;
}

这篇关于错误:''之前的预期主表达式。令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆