需要帮助计算增益.遇到错误.请帮助

Need help calculating the gain. running into an errror. pls need assistance

本文关键字:帮助 遇到 错误 计算      更新时间:2023-10-16

我的程序是一个自动股票市场,它从文件中读取数据并将其打印在屏幕上或写入文件。 我能够读取文件并将其显示在屏幕上,但是我在尝试计算增益时遇到了错误。下面是我的代码:

istream& operator>>(istream& ins, stockType& stock)
 {//member variables are all declared as a double
   ins>>stock.todays_open_price
      >>stock.todays_close_price
      >>stock.todays_high_price
      >>stock.prev_low_price
      >>stock.prev_close_price;
      calculateGain(stock.todays_close_price, stock_prev_close_price);
      return ins;
  }
 void stockType::calculateGain(double close, double prev)
        {  // gain was declared in the header file as a private member
           //variable to store the gain calculated.
           gain = ((close-prev)/(prev));
         }
  ostream& operator<<(ostream& outs, const stockType& stock)
  {        
     outs<<stock.getOpenprice()
         <<stock.getCloseprice()
         <<stock.getPrevLowPrice()
         <<stock.getPrevClosePrice()
         <<stock.getGain()
         return outs
   }
    //double getGain() was declared in the header file also as
     double getGain() {return gain;}

下面是我得到的错误:stockType.cpp: 在函数 'std::ifstream& operator>>(std::ifstream&, stockType&)':stockType.cpp:38:错误:"计算增益"未在此范围内声明

函数calculateGain是类stockType的成员;它是stockType的一个实例可以做的事情。重载的operator>>(不是stockType的成员)在没有此类实例的情况下调用它,这是非法的。试试这个:

istream& operator>>(istream& ins, stockType& stock)
{
  ...
  stock.calculateGain(stock.todays_close_price, stock.prev_close_price);
  ...
}