如何使用非静态函数计算用户输入
How to calculate user input using non-static functions
我的测试环境:Visual Studio2010 Ultimate.
我收到的错误:
main.cpp(39): error C2352: 'Account::DepositAmt' : illegal call of non-static member function
我尝试将其更改为静态函数,但它只会导致更多错误。
account.cpp (23): error C2597: illegal reference to non-static member 'Account::balance'
account.cpp(40): error C3867: 'Account::balance': function call
missing argument list; use '&Account::balance' to create a pointer to
member
我尝试使用尝试引用的建议进行修复,但失败得很惨。
objective,以便能够获取用户输入的初始余额,然后如果用户正在存款,则将其添加到余额中,如果用户正在取款,则减去它,然后打印最终余额。我需要创建成员函数
我只想弄清楚如何在不必更改为静态函数的情况下实现这一点(如果可能的话)。
这是我的
代码
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;
}
您应该首先获取用户对变量的输入,然后将其传递给方法:
cin >> amount;
deposit = account.DepositAmt(amount);
我的测试环境:Visual Studio2010 Ultimate.
我收到的错误:
main.cpp(39): error C2352: 'Account::DepositAmt' : illegal call of non-static member function
我尝试将其更改为静态函数,但它只会导致更多错误。
account.cpp (23): error C2597: illegal reference to non-static member 'Account::balance' account.cpp(40): error C3867: 'Account::balance': function call missing argument list; use '&Account::balance' to create a pointer to member
我尝试使用尝试引用的建议进行修复,但失败得很惨。
objective,以便能够获取用户输入的初始余额,然后如果用户正在存款,则将其添加到余额中,如果用户正在取款,则减去它,然后打印最终余额。我需要创建成员函数
我只想弄清楚如何在不必更改为静态函数的情况下实现这一点(如果可能的话)。
这是我的
代码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;
}
您应该首先获取用户对变量的输入,然后将其传递给方法:
cin >> amount;
deposit = account.DepositAmt(amount);