我不敢相信我有这个问题...请帮助! [英] I can't believe I'm having this problem...PLEASE HELP!

查看:50
本文介绍了我不敢相信我有这个问题...请帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,非常简单的问题。我正在尝试通过引用传递来调用函数

来更新值,但它不会更新该值。简而言之,我试图更新的

值是余额,它是

类帐户的私人成员。我有一个名为getBalance()的公共函数。我有另一个名为deposit的公共函数,我通过余额(通过引用传递来调用

getBalance())和第二个值

存款。在deposit()函数内部,如果我打印余额的值

加上要存入的金额,它会打印正确的值,即当前

余额加上存款金额。但是,它并没有更新Account类的余额成员

,因为当我调用getBalance()时,它仍然会在存款前显示原始值

。我知道我很遗憾这里很简单,但我只是看不到它。另外,我收到以下

关于使用传递参考的函数调用的警告,但是我不知道
了解它..


report4_q3.cpp:在函数`int main()''中:

report4_q3.cpp:20:警告:初始化非const引用`double&''

来自rvalue`double''

report4_q3a.cpp:21:警告:传递参数1

`Account :: deposit(double& amp; amp ;,double)''

report4_q3.cpp:27:警告:初始化非const引用`double&''

来自rvalue`double''

report4_q3a.cpp:29:警告:传递参数1

`Account :: withdraw(double&,double)''

这里是.h文件....(report4_q3.h)

使用命名空间std;


类账号

{

私人:

双倍余额;


公开:

帐户(); //默认构造函数

double getBalance();

void setBalance(double);

void deposit(double&,double);

撤销撤销(双&,双);

~Account(); //析构函数

};


这里是实现文件...(report4_q3a.cpp)

#include < iostream>

#include< string>


#include" report4_q3.h"


using namespace std;


//默认构造函数

账户::账户()

{

//给出500美元的起始余额

余额= 500.00;

}


double Account :: getBalance()

{

返还余额;

}


无效账户::存款(双& bal,双重存款)

{

cout<< endl<<"存款!"<< endl;

bal + = deposit;

cout<<"在交易余额为:$"<<< bal<< endl; //这当然显示

正确的余额

}


无效账户::取款(双倍和退出,双倍取款) )

{

cout<<<"提款!"<< endl;

bal - =提款;

cout<<"你的新余额是$"<<< getBalance()<< endl;

}


//析构函数

账户::〜账户()

{

//什么都不做

}


这里是带有main的文件...(report4_q3.cpp)

#include" report4_q3a.cpp"


使用命名空间std;


int main()

{

帐户a;

int inputVal = 0;

double amount = 0.0;


cout<<"您的余额为:$"< ;< a.getBalance()<< endl;

cout<< endl<<"按1进行存款,按2进行提款,3为

账户余额,4退出。&quo t;<< endl;

cin>> inputVal;


开关(inputVal)

{

案例1:{

cout<<"输入存款金额:" ;;

cin>>金额;

a.deposit(a.getBalance(),amount);

cout<<"你的新余额是$"<< a.getBalance()<< ; ENDL; //就在这里它

显示原始余额不正确

}

休息;

案例2: {

cout<<"输入提款金额:" ;;

cin>>金额;

a.withdrawal( a.getBalance(),金额);

}

休息;

案例3:cout<<"你的余额是:$ "<<< a.getBalance()<< endl;

break;

case 4:return 0;

break ;

默认值:cout<<"您输入的值无效 - 请重试。<< endl;

}


返回0;

}


任何帮助都非常感谢!

谢谢!

SB

Ok, very simple problem. I''m trying to update a value by calling a function
using pass by reference, but it does not update the value. In short, the
value I''m trying to update is balance, which is a private member of the
class Account. I have a public function called getBalance(). I have another
public function called deposit, which I pass the balance (by calling
getBalance() using pass by reference) and a second value for the amount of
deposit. Inside the deposit() function if I print the value of the balance
plus the amount to be deposited, it prints the right value, i.e. current
balance plus deposit amount. However, it''s not updating the balance member
of the Account class because when I call the getBalance(), it still shows
the original value before the deposit. I know I''m missing something very
simple here, but I just can''t see it. Also, I''m getting the following
warning regarding the function calls using pass by reference, but I don''t
understand it..

report4_q3.cpp: In function `int main()'':
report4_q3.cpp:20: warning: initialization of non-const reference `double &''
from rvalue `double''
report4_q3a.cpp:21: warning: in passing argument 1 of
`Account::deposit(double &, double)''
report4_q3.cpp:27: warning: initialization of non-const reference `double &''
from rvalue `double''
report4_q3a.cpp:29: warning: in passing argument 1 of
`Account::withdrawal(double &, double)''
Here''s the .h file....(report4_q3.h)
using namespace std;

class Account
{
private:
double balance;

public:
Account(); // default constructor
double getBalance();
void setBalance(double);
void deposit(double&, double);
void withdrawal(double&, double);
~Account(); // destructor
};

Here''s the implementation file...(report4_q3a.cpp)
#include <iostream>
#include <string>

#include "report4_q3.h"

using namespace std;

// default constructor
Account::Account()
{
// give a starting balance of $500
balance = 500.00;
}

double Account::getBalance()
{
return balance;
}

void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is: $"<<bal<<endl; //this of course shows
the correct balance
}

void Account::withdrawal(double& bal, double withdrawal)
{
cout<<"making a withdrawal!"<<endl;
bal -= withdrawal;
cout<<"Your new balance is $"<<getBalance()<<endl;
}

// destructor
Account::~Account()
{
// do nothing
}

Here''s the file with main...(report4_q3.cpp)
#include "report4_q3a.cpp"

using namespace std;

int main()
{
Account a;
int inputVal = 0;
double amount = 0.0;

cout<<"Your balance is: $"<<a.getBalance()<<endl;
cout<<endl<<"Press 1 to make a deposit, Press 2 to make a withdrawal, 3 for
account balance and 4 to quit."<<endl;
cin>>inputVal;

switch(inputVal)
{
case 1: {
cout<<"Enter the amount to deposit: ";
cin>>amount;
a.deposit(a.getBalance(), amount);
cout<<"Your new balance is $"<<a.getBalance()<<endl; // right here it
shows the original balance which is incorrect
}
break;
case 2: {
cout<<"Enter the amount to withdrawal: ";
cin>>amount;
a.withdrawal(a.getBalance(), amount);
}
break;
case 3: cout<<"Your balance is: $"<<a.getBalance()<<endl;
break;
case 4: return 0;
break;
default: cout<<"You have entered an invalid value - try again."<<endl;
}

return 0;
}

Any help is GREATLY appreciated!
Thanks!
SB

推荐答案

500

余额= 500.00;

}


双账号:: getBalance()

{

返还余额;

}


无效账户::存款(双& bal,双重存款)

{

cout<<< endl<<"存款!"<< endl;

bal + =存款;

cout< ;<"在交易余额为:
500
balance = 500.00;
}

double Account::getBalance()
{
return balance;
}

void Account::deposit(double& bal, double deposit)
{
cout<<endl<<"making a deposit!"<<endl;
bal += deposit;
cout<<"after transaction balance is:


"<< bal<< endl; //这当然显示

正确的余额

}


无效账户::取款(双倍和退出,双倍取款) )

{

cout<<<"提款!"<< endl;

bal - =提款;

cout<<"你的新余额是
"<<bal<<endl; //this of course shows
the correct balance
}

void Account::withdrawal(double& bal, double withdrawal)
{
cout<<"making a withdrawal!"<<endl;
bal -= withdrawal;
cout<<"Your new balance is


"<< getBalance()<< endl;

}


//析构函数

账户::〜账户()

{

/什么都不做

}


这里是带有main的文件...(report4_q3.cpp)

#include " report4_q3a.cpp"


使用命名空间std;


int main()

{

账户a;

int inputVal = 0;

double amount = 0.0;


cout<< 您的余额是:
"<<getBalance()<<endl;
}

// destructor
Account::~Account()
{
// do nothing
}

Here''s the file with main...(report4_q3.cpp)
#include "report4_q3a.cpp"

using namespace std;

int main()
{
Account a;
int inputVal = 0;
double amount = 0.0;

cout<<"Your balance is:


这篇关于我不敢相信我有这个问题...请帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆