这是我的bankAccount类的实现文件。因此,由于某种原因,错误消息不断指出我的函数getAccountNumber(),其中出现“ return accountNumber”作为问题。我在该程序中还有其他类,但是我不知道为什么它特别指出“ return accountNumber”命令是问题所在。我首先尝试对我的主函数中的数组的每个元素使用nullptr初始化指针数组,但这似乎不起作用。任何帮助将不胜感激。
//
// impBankAccount.cpp
// Project 3 Bank Account
//
// Created by user on 10/20/18.
// Copyright © 2018 user. All rights reserved.
//
#include <stdio.h>
#include <string>
#include <iomanip>
#include "bankAccount.h"
#include <iostream>
bankAccount :: bankAccount (string n, int acctNumber, double bal){
name = n;
accountNumber = acctNumber;
balance = bal;
}
int bankAccount :: getAccountNumber(){
return accountNumber;
}
double bankAccount:: getBalance() {
return balance;
}
string bankAccount:: getName() {
return name;
}
void bankAccount:: setName(string k){
name = k;
}
void bankAccount:: withdraw(double k) {
balance -= k;
toString = to_string(k);
toString.erase (toString.find_last_not_of('0') + 3, string::npos );
transactionsHistory[currentMonth] += "Withdrawal made: $" + toString + "\n";
}
void bankAccount:: deposit(double l){
balance += l;
toString = to_string(l);
toString.erase (toString.find_last_not_of('0') + 3, string::npos );
transactionsHistory[currentMonth] += "Deposit Made: $" + toString + "\n";
}
void bankAccount:: print() {
cout << "Name: " << name << endl;
cout << "Account Balance: " << balance << endl ;
cout << "Account Number: " << accountNumber << endl;
}
void bankAccount:: setCurrentMonth(int month){
currentMonth = month;
}
string bankAccount :: getAccountType(){
return "Bank Account";
}
bankAccount :: ~bankAccount() {
}
这是我主文件的代码:
#include <iostream>
#include <iomanip>
#include "bankAccount.h"
#include "savingsAccount.h"
#include "highInterestSavings.h"
#include "noServiceChargeChecking.h"
#include "serviceChargeChecking.h"
#include "highInterestChecking.h"
#include "certificateOfDeposit.h"
#include "checkingAccount.h"
void program(bankAccount *[]);
int getAccount(bankAccount *[], int);
/*
Account Types:
Bank Account
Checking
Service Charge Checking
Checking with no service charge
High Interest Checking
Certificate of Deposit
Savings
High Interest Savings
*/
using namespace std;
int main()
{
bankAccount *accountsList[6];
program(accountsList);
accountsList[0] = new savingsAccount("Bill", 10200, 2500);
accountsList[1] = new highInterestSavings("Susan", 10210, 2000);
accountsList[2] = new noServiceChargeChecking("John", 20100,3500);
accountsList[3] = new serviceChargeChecking("Ravi", 30100, 1800);
accountsList[4] = new highInterestChecking("Sheila", 20200, 6000);
accountsList[5] = new certificateOfDeposit("Hamid", 51001, 18000, 0.075, 18);
}
void program(bankAccount *accountList[]){
int acct = 0;
int num = 0;
cout << "What is your account number?";
cin >> acct;
num = getAccount(accountList, acct);
cout << "Welcome to your " << accountList[num]->getAccountType() << " account!";
} // END MAIN
int getAccount(bankAccount *accountList[], int acct){
int num = 0;
for(int i =0; i<6; i++){
if (accountList[i]->getAccountNumber() == acct) {
num = i;
break;
}
}
return num;
}
答案 0 :(得分:1)
因为在accountsList
中调用accountsList[i]->getAccountNumber
时getAccount
未初始化。
也许您打算先填充它,然后然后呼叫program
?