2015-09-25 150 views
-8

的錯誤是:C++:不被忽略,因爲它空值應該是

estimate1 = leibnizPi (nTerms, estimatedV1); 

&

estimate2 = wallisPi (nTerms, estimatedValue2); 

我想它有它設置的方式做引用函數中的estimatedValue,或者它被調用的方式不正確。

任何幫助非常感謝!

注意:必須保持VOID。對於那個很抱歉。

#include <iostream> 
#include <cmath> 

// 
// This program will be used in the second assignment (functions) 
// 

using namespace std; 

const double PI = 3.14159265358979323846; 


void leibnizPi (int numberofterms, double &estimatedValue1) 
{ 

    double sign = 1.0; 
    double sum = 0.0; 

    for (int i = 0; i < numberofterms; ++i) { 
     double denominator = 2.0 * i + 1.0; 
     double term = 4.0/denominator; 
     sum = sum + sign * term; 
     sign = -sign; 
    } 
    estimatedValue1 = sum; 
} 

void wallisPi (int numberofterms, double &estimatedValue2) 
{ 
    double product = 1.0; 

    for (int i = 1; i < numberofterms; ++i) { 
     double r = 2.0*i; 
     r = r*r; 
     double term = r/(r-1.0); 
     product = product * term; 
    } 
    estimatedValue2 = 2.0 * product; 

} 


double abstractError (double computedValue); 

double relativeError (double computedValue); 

int main (int argc, char** argv) { 
    double estimate1 = 0; 
    double absErr1 = 0; 
    double relErr1 = 0; 
    double estimate2 = 0; 
    double absErr2 = 0; 
    double relErr2 = 0; 
    double estimatedV1 = 0; 
    double estimatedValue2 = 0; 

    for (int nTerms = 1; nTerms < 100001; nTerms = nTerms * 4) { 
     // Estimate Pi by two different methods 

     // Leibniz' sum 
     estimate1 = leibnizPi (nTerms, estimatedV1); 
     absErr1 = abstractError (estimate1); 
     relErr1 = relativeError (estimate1); 

     // Wallis' product 
     estimate2 = wallisPi (nTerms, estimatedValue2); 
     absErr2 = abstractError (estimate2); 
     relErr2 = relativeError (estimate2); 

     cout << "After " << nTerms << " terms\n"; 
     cout << "Leibniz' estimate: "<< estimate1 << "\n"; 
     cout << "Absolute error: " << absErr1 
      << "\tRelative error: " << relErr1 
      << "\n"; 

     cout << "Wallis' estimate: "<< estimate2 << "\n"; 
     cout << "Absolute error: " << absErr2 
      << "\tRelative error: " << relErr2 
      << "\n"; 

     cout << endl; 
    } 
    return 0; 

} 

double abstractFunction (double computedValue) 
{ 
    double abstractError = abs(computedValue - PI); 
    return abstractError; 
} 

double relativeFunction (double computedValue){ 
    double relativeError1 = abs(computedValue - PI)/PI; 
    return relativeError1; 
} 
+0

提高警告等級。 –

+0

你說這些函數是「賦值」,並且必須保持爲「空」。謹慎澄清?你想要做什麼? –

+0

在變量估計值1分配一個空值後,你期望的是什麼? –

回答

0

沒有,問題是,你還沒有定義的函數返回任何東西,比你試圖把它們返回的內容(這是不確定的),並將其分配給一個變量,這是一個錯誤。

把它定義爲這樣double leibnizPi (int numberofterms, double &estimatedValue1)

,並添加一個return語句。

如果您不能更改函數的返回類型,請不要試圖將其視爲返回值。只需編寫leibnizPi (nTerms, estimatedV1);而不是estimate1 = leibnizPi (nTerms, estimatedV1);

+0

是的,這是非常有意義的,但這是爲了一個任務,所以它必須保持無效功能。 –

+0

@JakeAyers,請參閱我的編輯 – StoryTeller

2

不能使用返回void的函數的返回值,因爲不存在一個返回值。相反,你可能會想嘗試這樣的:

double leibnizPi (int numberofterms, double &estimatedValue1) 
{ 
    double sign = 1.0; 
    double sum = 0.0; 

    for (int i = 0; i < numberofterms; ++i) { 
     double denominator = 2.0 * i + 1.0; 
     double term = 4.0/denominator; 
     sum = sum + sign * term; 
     sign = -sign; 
    } 
    estimatedValue1 = sum; 
    return estimatedValue1; 
} 

double wallisPi (int numberofterms, double &estimatedValue2) 
{ 
    double product = 1.0; 

    for (int i = 1; i < numberofterms; ++i) { 
     double r = 2.0*i; 
     r = r*r; 
     double term = r/(r-1.0); 
     product = product * term; 
    } 
    estimatedValue2 = 2.0 * product; 
    return estimatedValue2; 
} 

如果必須使用void功能,您應該指定作爲參數(estimatedV1)到二級變量(estimate1)傳遞的變量。像這樣:

leibnizPi (nTerms, estimatedV1); 
estimate1 = estimatedV1; 
+0

是的,但它很遺憾必須保持無效函數,因此我試圖將它用作參考。 –

相關問題