2015-09-25 151 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

是的,但它很遗憾必须保持无效函数,因此我试图将它用作参考。 –

相关问题