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

时间:2013-09-28 23:51:55

标签: c++

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

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&amp;运算符&gt;&gt;(std :: ifstream&amp;,stockType&amp;)': stockType.cpp:38:错误:未在此范围内声明'calculateGain'

1 个答案:

答案 0 :(得分:0)

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

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