C++:无效值不能被忽略,因为它应该被忽略

C++:无效值不能被忽略,因为它应该被忽略,c++,function,reference,arguments,C++,Function,Reference,Arguments,错误在于: estimate1 = leibnizPi (nTerms, estimatedV1); & 我认为这与函数中引用estimatedValue的设置方式有关,或者调用它的方式不正确 非常感谢您的帮助 注:必须保持无效。很抱歉 #include <iostream> #include <cmath> // // This program will be used in the second assignment (functions) // using n

错误在于:

estimate1 = leibnizPi (nTerms, estimatedV1);
&

我认为这与函数中引用estimatedValue的设置方式有关,或者调用它的方式不正确

非常感谢您的帮助

注:必须保持无效。很抱歉

#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;
}

不,问题是你没有定义你的函数来返回任何东西,然后你试图获取它们返回的未定义的东西并将其分配给变量,这是一个错误

将其定义为double leibnizPi int numberofterms、double&estimatedValue1

并添加一个return语句


如果无法更改函数的返回类型,则不要尝试将其视为返回值。只需写下莱布尼茨指数,估计dv1;代替estimate1=leibnizPi entrems,而是estimatedV1

不能使用返回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;
}

启动警告级别。你说这些函数是分配的,必须保持无效。想澄清一下吗?你到底想做什么?在变量estimate1赋值后,你期望它是什么?你试过使用指针吗?是的,这很有道理,但这是赋值,所以它必须保持一个空函数。@JakeAyers,请参阅我的editYes,但遗憾的是,它仍然是一个空函数,因此我尝试将其用作引用。
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;
}
leibnizPi (nTerms, estimatedV1);
estimate1 = estimatedV1;