2012-03-17 3 views
0

저는 아주 기본적인 것을 간과하고 있습니다. 누구라도 도움을 줄 수 있거나 관련 스레드로 나를 안내 할 수 있다면 매우 감사 할 것입니다. 또한, 더 많은 코드가 필요하다면, 나는 그것을 제공하게되어 기쁩니다.C++ 클래스간에 객체 전달 문제가 발생했습니다.

나는 간단한 은행 계좌 프로그램을 가지고 있습니다.

void deposit(const Bank bank, ofstream &outfile) 
{ 
    int requested_account, index; 
    double amount_to_deposit; 
    const Account *account; 

    cout << endl << "Enter the account number: ";   //prompt for account number 
    cin >> requested_account; 

    index = findAccount(bank, requested_account); 
    if (index == -1)          //invalid account 
    { 
     outfile << endl << "Transaction Requested: Deposit" << endl; 
     outfile << "Error: Account number " << requested_account << " does not exist" << endl; 
    } 
    else             //valid account 
    { 
     cout << "Enter amount to deposit: ";    //prompt for amount to deposit 
     cin >> amount_to_deposit; 

     if (amount_to_deposit <= 0.00)      //invalid amount to deposit 
     { 
      outfile << endl << "Transaction Requested: Deposit" << endl; 
      outfile << "Account Number: " << requested_account << endl; 
      outfile << "Error: " << amount_to_deposit << " is an invalid amount" << endl; 
     } 
     else            //valid deposit 
     { 
      outfile << endl << "Transaction Requested: Deposit" << endl; 
      outfile << "Account Number: " << requested_account << endl; 
      outfile << "Old Balance: $" << bank.getAccount(index).getAccountBalance() << endl; 
      outfile << "Amount to Deposit: $" << amount_to_deposit << endl; 
      bank.getAccount(index).makeDeposit(&amount_to_deposit);  //make the deposit 
      outfile << "New Balance: $" << bank.getAccount(index).getAccountBalance() << endl; 
     } 
    } 
    return; 
} // close deposit() 

문제가 makeDeposit (& amount_to_deposit)에 있습니다 : 주() 클래스에서 나는 다음과 같은 기능을 가지고있다. 출력 파일을 보여줍니다

void Account::makeDeposit(double *deposit) 
{ 
    cout << "Account balance: " << accountBalance << endl; 
    accountBalance += (*deposit); 
    cout << "Running makeDeposit of " << *deposit << endl; 
    cout << "Account balance: " << accountBalance << endl; 
    return; 
} 

콘솔 출력은 cout과 통화에서입니다 :

Account 클래스 내에서
Transaction Requested: Deposit 
Account Number: 1234 
Old Balance: $1000.00 
Amount to Deposit: $168.00 
New Balance: $1000.00 

, 여기에 기능 makeDeposit입니다 그래서

Enter the account number: 1234 
Enter amount to deposit: 169 
Account balance: 1000 
Running makeDeposit of 169 
Account balance: 1169 

, makeDeposit() 함수 내에서 accountBalance 변수를 올바르게 업데이트하고 있습니다. 그러나 함수가 끝나면 초기 값으로 되돌아갑니다.

필자는 경험 많은 프로그래머를위한 기본 요소라고 확신합니다. 당신의 통찰력은 대단히 감사 할 것입니다.

감사합니다, 아담

답변

3

그렇지 참조 및 const를 같은 값으로 은행을 전달하는 때문입니다.

void deposit(Bank& bank, ofstream &outfile) 

void deposit(const Bank bank, ofstream &outfile) 

을 변경하면 여전히 트릭을 수행하지 않는 몇 가지 이유로,

+0

을 수정해야합니다. 나는 모든 사람들이 당신의 대답에 동의하고 있음을 알았습니다. 그것은 제 문제가 제 마지막에 있다고 생각하게 만듭니다. 다른 제안? –

+0

in line : bank.getAccount (index)는 참조, 포인터 또는 값에 의한 계정 반환입니까? –

+0

참고로 반환됩니다. 구현은 다음과 같습니다. –

관련 문제