2015-05-14 111 views
0

我有一个名为更新帐户的虚拟方法,其中基于从find方法找到的指针并返回到main,该帐户将相应地更新。无法更新对象的属性?

有一个叫做Account的父类,其中Savings来源于。

但是,储蓄不会输出更新的利息,也不会使利息生效。

所有账户有余额和存款不会账户之间改变,这就是为什么我打电话帐户的存款方式

void Savings::UpdateAccount(Date *date) 
{ 
    int interestMonths; 
    short lastYear; 
    short lastMonth; 
    float currBal; 

    //THESE GET THE CURRENT DATE - Differs from the account creation 
    //date and allows for the calculation of interest 

    lastMonth = GetAccountMonth(); 
    lastYear = GetAccountYear(); 
    currBal = GetAccountBal(); 

     if (((date -> GetYear () - lastYear) * 12 + 
      (date -> GetMonth () - lastMonth)) > 0) 
     { 
      interestMonths = ((date -> GetYear () - lastYear) * 12 + 
          (date -> GetMonth () - lastMonth)); 

      for (int index = 0; index < interestMonths; index++) 
      { 
       currBal = currBal + (currBal * interestRate); 
      } 

      //This method takes the calculated current balance, then 
      //passes it into the parent class method to update the 
      //private accountBal attribute. 

      SetBalance(currBal); 
     } 
} 

的问题是,这种方法没有更新的对象和我的平衡我相当确定我的利率计算不是问题。

谢谢你的帮助 - 这种方法现在可以工作。

+1

一般来说,你可以这样做,是的。但是你发布的代码似乎不是你问题的根源,不管这些应该是什么。 –

+0

那么如果没有找到帐户会发生什么?提示调用者将得到一个指针,它不可能知道是对还是错,并尝试对其进行解引用。 –

+0

@ lovestogame227你刚刚编辑了这个问题。请注意:这样做会使所有过去的答案和评论显得无关紧要。 – Cinch

回答

2

您正在更新a余额,但存在错误的帐户。

void Savings::UpdateAccount(Date *date)const 
{ 
    int interestMonths; 
    short lastYear; 
    short lastMonth; 
    float currBal; 
    Account myAccount; 

这里,myAccount是一个局部变量,无关的帐户,您刚刚发现(这是this)...

myAccount.SetBalance(currBal); 

...并且它是账户余额,你”重新更新。

你要修改你调用的函数的对象,所以才说

SetBalance(currBal); 

,并从功能删除const - 你不能有更新的帐户,但没有按”功能不要修改它。

你也不需要添加“储蓄::”一Savings成员的定义里面 -

lastMonth = GetAccountMonth(); 
lastYear = GetAccountYear(); 
currBal = GetAccountBal(); 

应该只是罚款。

+0

试过,但想出这个错误:多个标记在该行 \t - 过客“常量储蓄”作为“本”的说法“无效帐户::的setBalance(浮动)” \t丢弃预选赛[-fpermissive] \t - 无效参数'候选人是:无效SetBalance(浮动)'编辑:我没有完全读你的评论,只是删除了常量。谢谢你。 – lovestogame227

+1

@ lovestogame227从函数中删除'const'。 – molbdnilo

+0

@molbdnilo:你应该更新你的答案。用'const'方法试图修改对象会是不好的;-) –