2013-09-28 168 views
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:在函數 '的std :: ifstream的&操作>>(性病:: ifstream的&,stockType &)': stockType .cpp:38:錯誤:'calculateGain'未在此範圍內聲明

+0

它是stockType的一個成員。我在頭文件中聲明它。 – Emy

回答

0

函數calculateGain是類stockType的成員;這是stockType可以做的一個實例。超載的operator>>(它不是stockType的成員)在沒有這種非法的情況下調用它。試試這個:

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

好的,謝謝它的工作 – Emy

+0

hello beta,如果我想在calculateGain()範圍內將增益舍入到2個小數點。我怎麼去那 – Emy

+0

@Emy:1)學會把一個'double'加到'int'上,並且注意它被截斷,2)學會使用它來* round *,而不是截斷,3)學習舍入到最接近的0.01而不是最接近的整數。 – Beta