c++ - 错误 : expected primary-expression before '.' token

标签 c++

好吧,我在学习这个我应该为学校做的 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;
}

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

最佳答案

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

关于c++ - 错误 : expected primary-expression before '.' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15958577/

相关文章:

c++ - binary_search 通过其成员函数的返回变量查找类对象 [c++]

c++ - 指向不可变类型的共享指针具有值语义

c++ - 使用嵌套的 boost::binds

c++函数返回一个枚举?

c++ - 模板类和 typedef

c++ - Boost状态图-使用状态图作为模板参数时出现编译错误

c++ - 未写入 vector 的值

c++ - 在 C++ 中快速将原始数据转换为十六进制字符串

c++ - BOOST_TEST_PASSPOINT 有什么作用?

c++ - DirectX 11 获取显卡的描述