【问题标题】:"[Error] ld returned 1 exit status" on function函数上的“[错误] ld 返回 1 个退出状态”
【发布时间】:2017-10-14 17:34:33
【问题描述】:

我正在尝试编写一个程序来使用函数计算二次方程的输出,但 Dev C++ 不断给我输出“[Error] Id 返回 1 退出状态”。 我是 C++ 的新手,如果我犯了一些愚蠢的错误,请提前道歉。

#include <iostream>
#include <cmath>
using namespace std;

float equation1 (float, float, float);
float equation2 (float, float, float);

main()
{
    float a, b, c, Res1, Res2;
    cout << "Insert the parameters of the equation.\n";
    cin >> a >> b >> c;
    if (a == 0)
    {
        Res1 = b / c;
        cout << "It's a 1st degree eq. and the result is " << Res1 << endl;
    }
    else
    {
        Res1 = equation1 (a, b, c);
        Res2 = equation2 (a, b, c);
        cout << "The results of the eq. are " << Res1 << " and " << Res2 << endl;
    }
    system ("pause");
    return 0;
}

float equation1 (double a, double b, double c)
{
    float D, Res1;
    D = (b * b) - 4 * a * c;
    Res1 = (- b + sqrt(D)) / (2 * a);
    return Res1;
}

float equation2 (double a, double b, double c)
{
    float D, Res2;
    D = (b * b) - 4 * a * c;
    Res2 = (- b - sqrt(D)) / (2 * a);
    return Res2;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。
  • 我想你可能打算写int main(){ ...}。其次,链接器找不到equation1(float, float, float)的定义,同样equation2(float, float, float)。请注意,定义的函数 float equation1(double, double, double) 重载了您的第一个声明。
  • @WhiZTiM 我应该怎么做才能避免过载?
  • ... 编译后你可能会注意到你没有检查c != 0(你会在Res1 = b / c;中得到一个除零)和D &gt;= 0在尝试获取它的@ 987654331@.
  • @Sam - 你在上面的声明中有float 参数和函数体所在的double 参数。

标签: c++ function equation dev-c++


【解决方案1】:

更改函数定义中的类型。您正在使用双精度,然后 C++ 期望使用浮点数(精度低于双精度)。

float equation1 (float a, float b, float c)
{
    float D, Res1;
    D = (b * b) - 4 * a * c;
    Res1 = (- b + sqrt(D)) / (2 * a);
    return Res1;
}

float equation2 (float a, float b, float c)
{
    float D, Res2;
    D = (b * b) - 4 * a * c;
    Res2 = (- b - sqrt(D)) / (2 * a);
    return Res2;
}

【讨论】:

  • 感谢大家的热心回答!我认为“float”和“double”可以视为相同的东西,我的错。
猜你喜欢
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多