Hello StackOverflowコミュニティ。 クラスの値を変更できないようです。 私はC++を初めて使っています。私は解決策を探していましたが、それを理解できないようです。C++。オブジェクトへのアクセスと値の設定
This is my Account.cpp
#include "Account.h"
#include <iostream>
using namespace std;
Account::Account(string accName, int accBalance)
{
name = accName;
balance = accBalance;
std::vector<std::string> Report();
}
bool Account::Deposit(Account a,int amt) {
if (amt >= 0) {
a.balance += amt;
return 1;
}
else
return 0;
}
bool Account::Withdraw(Account a, int amt) {
if (amt >= 0 && (a.balance - amt > 0)) {
a.balance -= amt;
return 1;
}
else
return 0;
}
int equal(string a, string b) {
if (a == b) return 1;
else return 0;
}
Account::~Account()
{
}
これは私デポジットを行った後、出力のアカウント情報、バランス値が0 で変化しないまま何らかの理由で任意の助けのための私のmain.cppに
int main() {
int aux_bal;
string aux_name;
int choice;
std::string InputName;
std::list<Account> arr;
std::list<Account>::iterator result;
do
{
cout << endl
<< " 1 - Create New Account.\n"
<< " 2 - View Balance.\n"
<< " 3 - Make a Deposit\n"
<< " 4 - Make a Withdraw\n"
<< " 5 - Check log\n"
<< " 6 - Exit.\n"
<< " Enter your choice and press return: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Input holder's account name: \n";
cin >> InputName;
arr.push_back(Account(InputName, 0));
break;
case 2:
cout << "Enter Account Name:\n";
cin >> InputName;
for (result = arr.begin(); result != arr.end(); result++) {
int aux_bal = result->balance;
std::string aux_name = result->name;
if (aux_name == InputName) {
cout <<"Account Balance:"<< aux_bal << endl;
cout << "Account Holder:" << aux_name << endl;
}
}
break;
case 3:
cout << "Enter Account Name:\n";
cin >> InputName;
for (result = arr.begin(); result != arr.end(); result++) {
int aux_bal = result->balance;
std::string aux_name = result->name;
if (aux_name == InputName) {
cout << "Enter ammount to Deposit:\n";
cin >> aux_bal;
(*result).Deposit(*result,aux_bal);
}
}
break;
case 4:
//code to help the user like give him
//extra information about the mode and the controller
break;
case 5:
break;
case 6:cout << "End of Program.\n";
break;
default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}
} while (choice != 6);
return 0;
}
してくださいますか?
参考までに渡す必要があります。値渡しとは、関数がオブジェクトの一時的なコピーを変更するだけで、変更が外部からは観測できないことを意味します。これは超基本的なC++なので、最初の例であなたの本に相談してください。しかし、もしあなたがSOスレッドを必要とするならば、たくさんあるでしょう。 [関数はC++のオブジェクト属性を変更しない](https://stackoverflow.com/questions/42915874/function-doesnt-change-object-attributes-in-c) –
短い回答: 'Account :: Deposit'と' Account :: Withdraw'は2番目の 'Account'を値で取るべきではなく、' this'を操作しなければなりません。長い答え:数値的なバランスで金融取引をモデル化しないでくださいが、トランザクションオブジェクトの合計として – Caleth
うわー、私は実際にそれらがインスタンスメソッドであったことに気づいていませんでした。それはさらに悪いことです。申し訳ありませんが、これは基本的なものです。このような質問をする前に本をお読みください。あなたの本がほとんどこれを最初に説明していないなら、より良いものを手に入れてください。 –