0

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

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: In function 'std::ifstream& operator>>(std::ifstream&, stockType&)': stockType.cpp:38: error: 'calculateGain' 没有在这个范围内声明

4

1 回答 1

0

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

istream& operator>>(istream& ins, stockType& stock)
{
  ...
  stock.calculateGain(stock.todays_close_price, stock.prev_close_price);
  ...
}
于 2013-09-29T00:20:11.590 回答