我的测试环境:Visual Studio 2010 Ultimate。
我收到的错误:
main.cpp(39):错误C2352:' Account :: DepositAmt' :非静态成员函数的非法调用
我尝试将其更改为静态功能,但只会导致更多错误。
account.cpp(23):错误C2597:非法引用非静态成员' Account :: balance' account.cpp(40):错误C3867:' Account :: balance':函数调用 缺少参数列表;使用'& Account :: balance'创建指针 构件
我已尝试使用尝试引用的建议进行修复但却失败了。
目标,能够获取初始余额的用户输入,然后如果用户正在进行存款,则将其添加到余额中,如果用户提取,则减去它,然后打印最终余额。我 必需 来创建成员函数
我想要的只是弄清楚如何实现这一目标而不必改为静态函数(如果可能的话)。
这是
的代码Account.h
#define ACCOUNT_H
#include <iostream>
#include <cstring>
using namespace std;
class Account {
public:
//Object constructor
Account(char firstName[], char lastName[], char sinNumber[], double balance, int accountType, int transactions);
//Object operations
double DepositAmt(double amount);
double WithdrawAmt(double amount);
void PrintStatement();
double getFinalBalance(double fbal);
private:
//Object properties
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance;
int accountType;
int transactions;
};
Account.cpp
#include "account.h"
//Initializing account entities
Account::Account(char fn[], char ln[], char sin[], double bal, int actype, int trans)
{
strcpy(firstName, fn);
strcpy(lastName, ln);
strcpy(sinNumber, sin);
balance = bal;
accountType = actype;
transactions = trans;
};
//Despoit amount to account of user, will return the balance now
double Account::DepositAmt(double amount)
{
if (amount < 0){
cout << "You cannot deposit a negative balance!" << endl;
return 0;
}
//this will be the balance now, and it adds balances, when we do a deposit
balance += amount;
return balance;
};
//Withdrawal amount from account of user
double Account::WithdrawAmt(double withdrawal)
{
//if the withdraw amount is 0 or in minus then return 0
if (withdrawal < 0)
{
cout<<"sorry we cannot process your withdraw at the moment, its either 0 or in minus"<<endl;
return 0;
};
//if what we are trying to withdraw is bigger than our balance, then it should fail too
if (withdrawal > balance)
{
cout<<"Sorry the withdraw amount exceeds the account balance"<<endl;
return 0;
};
//we deposited some amount in the deposite method, then we should have some money to withdraw
balance = balance - withdrawal;
return balance;
};
//Get final balance after withdrawal
double Account::getFinalBalance(double fbal)
{
//this will return the remaining amount
return balance;
};
//Print remaining balance
void Account::PrintStatement()
{
cout<<"First Name: "<<firstName<<endl;
cout<<"Last Name: " <<lastName<<endl;
cout<<"SIN Number: " <<sinNumber<<endl;
cout<<"Initial Balance: "<<balance<<endl;
cout<<"Account Type: "<<accountType<<endl;
cout<<"Transactions: "<<transactions<<endl;
};
主要的.cpp
#include <iostream>
#include <string>
#include "account.h"
using namespace std;
int main()
{
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance;
int accountType;
int transactions = 0;
//Retrieve client information
cout << "Please fill out the information below for registration:" << endl;
cout << "Enter first name: ";
cin.getline(firstName, 255);
cout << "Enter last name: ";
cin.getline(lastName, 255);
cout << "Enter SIN number: ";
cin.getline(sinNumber, 255);
cout << "Please enter your initial balance: ";
cin >> balance;
cout << "Enter account type:\n-1 for Chequing\n-2 for Savings\n:::::: ";
cin >> accountType;
cout << "Please wait..." << endl;
//Creating the account
Account account(firstName, lastName, sinNumber, balance, accountType, transactions);
double deposit;
double withdraw;
double amount;
cout << "Amount to deposit: ";
cin >> &Account::DepositAmt(deposit);
cout << "Your new balance is: " << deposit << endl;
}
答案 0 :(得分:2)
您应该首先获取变量的用户输入,然后将其传递给方法:
cin >> amount;
deposit = account.DepositAmt(amount);