【问题标题】:C++ error = "abs cannot be used as a function" (Group Class Project)C++ 错误 =“abs 不能用作函数”(组类项目)
【发布时间】:2015-02-07 19:14:52
【问题描述】:

我自己和另外两个人正在为我们的 C++ 课程做一个项目,但遇到了一个问题。该项目将在几天后到期,因此我将向任何人和所有人提出这个问题,以便在我们的截止日期之前解决它。我们收到错误“abs 不能用作函数”

能否请您查看我们的编码并给我们一些指导?谢谢!

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

int main()
{
    double slope, yIntercept, xCoord, yCoord, yCoordCalc, yCalcLow;
    double yCalcHigh, yCalcDifference, abs;

    cout << "This program verifies that a selected point is on 
             a given line." << endl;
    cout << "All input values may be integer or floating-point." << endl;
    cout << "Enter slope: " << endl;
    cin >> slope;
    cout << "Enter y-intercept: " <<endl;
    cin >> yIntercept;
    cout << "Enter coordinates of the point: x y " << endl;
    cin >> xCoord >> yCoord;

    //calculate the Y coordinate;
    yCoordCalc = slope * xCoord + yIntercept;

    //calculate 2% above & below the yCoordCalc;
     yCalcLow = yCoordCalc * .98;
    yCalcHigh = yCoordCalc * 1.02;
    //calculate the difference
    yCalcDifference = yCalcHigh - yCalcLow;


    //Now use absolute value to check it (delta reference);
    if (abs((yCalcLow + yCalcDifference) - yCalcHigh) < yCoord)
    {
        cout << "The point is on the line.";
        return 1;
    }
    else
    {
        cout << "The point is NOT on the line.";
        return 0;
    }
}

【问题讨论】:

    标签: c++ function compiler-errors


    【解决方案1】:

    你有一个变量

    double abs
    

    那是遮蔽函数

    std::abs
    

    1) 重命名变量
    2)Stop using using namespace std

    我会推荐这两个建议,而不仅仅是一个。

    【讨论】:

    • 对于这种特殊情况,我不能停止使用命名空间 std,否则我的老师会在我的项目中标记分数。我删除了变量名(因为我不需要那个变量),现在错误是“调用重载'(abs double)'是不明确的”
    • @Merlot 使用fabs 而不是abs
    • 感谢您的帮助!在您的帮助和持续研究下,我意识到我需要 1. 删除变量(根据您的建议) 2. 使用 cmath 标题和 3. 更改 yCalcDifference 的计算。它现在可以编译、运行并在链接到我的教练试驾时显示为准确。
    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2016-05-24
    • 2022-11-12
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多