【问题标题】:C++: void value not ignored as it ought to be [closed]C ++:不应忽略无效值,因为它应该被[关闭]
【发布时间】:2015-12-23 18:40:50
【问题描述】:

错误在于:

estimate1 = leibnizPi (nTerms, estimatedV1);

&

estimate2 = wallisPi (nTerms, estimatedValue2);

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

非常感谢任何帮助!

注意:必须保持无效。对此感到抱歉。

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

【问题讨论】:

  • 提高警告级别。
  • 您说这些功能是“用于分配”的,必须保持void。想澄清一下吗?你到底想做什么?
  • 在分配一个 void 值后,您期望变量 estimate1 中的内容是什么?
  • 你试过使用指针吗?

标签: c++ function reference arguments


【解决方案1】:

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

【讨论】:

  • 是的,但遗憾的是它必须保持一个 void 函数,因此我试图将它用作参考。
【解决方案2】:

不,问题是你没有定义你的函数来返回任何东西,而不是你试图把它们返回的东西(这是未定义的)分配给一个变量,这是一个错误。

这样定义double leibnizPi (int numberofterms, double &amp;estimatedValue1 )

并添加return语句。

如果你不能改变函数的返回类型,不要试图把它当作返回值。只需写leibnizPi (nTerms, estimatedV1); 而不是estimate1 = leibnizPi (nTerms, estimatedV1);

【讨论】:

  • 是的,这很有道理,但这是一个赋值,所以它必须保持一个 void 函数。
  • @JakeAyers,看看我的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多