重载 cmath 函数 C++ 时出现函数重载错误

Function Overloading Error When Overloading cmath function C++

我正在编写一个名为 Vector2f 的简单 C++ class。我有一个名为 Vector2f.cpp 的 class 文件和一个名为 Vector2f.h 的头文件。

我的 Vector2f class 有一个名为 abs 的函数,其中 returns 一个新的 Vector2f 具有原始每个组件的绝对值Vector2f。我正在使用 cmath 库。但是,当我尝试使用 cmath 的 abs 函数时,它认为我指的是一些未定义的函数 Vector2f::abs(float) 而不是 cmath 的 abs 函数。为什么这里会出现命名冲突? C++ 不应该能够解决一个名为 abs 的函数只在 cmath 中定义而不是在 Vector2f.h

中定义的浮点数

这是我的代码:

我的头文件:

//Vector2f.h

#ifndef VECTOR2F_H
#define VECTOR2F_H

class Vector2f
{
private:
    float x;
    float y;

public:
    Vector2f(float x, float y);
    float length();
    float dot(Vector2f r);
    Vector2f normalized();
    Vector2f rot(float angle);
    Vector2f add(Vector2f r);
    Vector2f add(float r);
    Vector2f sub(Vector2f r);
    Vector2f sub(float r);
    Vector2f mul(Vector2f r);
    Vector2f mul(float r);
    Vector2f div(Vector2f r);
    Vector2f div(float r);
    Vector2f abs();
    float getX();
    float getY();

};

#endif // VECTOR2F_H

我的 class 文件:

//Vector2f.cpp

#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif // M_PI
#include "Vector2f.h"
#include <cmath>

Vector2f::Vector2f(float x, float y)
{
    this -> x = x;
    this -> y = y;
}
float Vector2f::length()
{
    return (float)sqrt(x * x + y * y);
}

float Vector2f::dot(Vector2f r)
{
    return x * r.getX() + y * r.getY();
}

Vector2f Vector2f::normalized()
{
    float length = Vector2f::length();

    float xnormal = x/length;
    float ynormal = y/length;

    return Vector2f(xnormal, ynormal);
}

Vector2f Vector2f::rot(float angle)
{
    double rad = angle * M_PI / 180.0;
    double c = cos(rad);
    double s = sin(rad);

    return Vector2f((float)(x * c - y * s), (float)(x * s + y * c));
}

Vector2f Vector2f::add(Vector2f r)
{
    return Vector2f(x + r.getX(), y + r.getY());
}

Vector2f Vector2f::add(float r)
{
    return Vector2f(x + r, y + r);
}

Vector2f Vector2f::sub(Vector2f r)
{
    return Vector2f(x - r.getX(), y - r.getY());
}

Vector2f Vector2f::sub(float r)
{
    return Vector2f(x - r, y - r);
}
Vector2f Vector2f::mul(Vector2f r)
{
    return Vector2f(x * r.getX(), y * r.getY());
}

Vector2f Vector2f::mul(float r)
{
    return Vector2f(x * r, y * r);
}
Vector2f Vector2f::div(Vector2f r)
{
    return Vector2f(x / r.getX(), y / r.getY());
}

Vector2f Vector2f::div(float r)
{
    return Vector2f(x / r, y / r);
}
Vector2f Vector2f::abs()
{
    //I get the error, "error: no matching function for call to 'Vector2f::abs(float&)'", here
    //when trying to call abs(x) and abs(y)
    float xabs = abs(x); 
    float yabs = abs(y);
    return Vector2f(xabs, yabs);
}
float Vector2f::getX()
{
    return x;
}
float Vector2f::getY()
{
    return y;
}

我的主文件:

//main.cpp

#include <iostream>
#include "Vector2f.h"

using namespace std;

int main()
{
    Vector2f a(1.0f,2.0f);

    cout<<a.getX()<<','<<a.getY()<<endl;

    cout<<a.abs()<<endl;

    return 0;
}

如有任何帮助,我们将不胜感激。

编辑:

错误信息:

error: no matching function for call to 'Vector2f::abs(float&)'

at line 80:

float xabs = abs(x);

error: no matching function for call to 'Vector2f::abs(float&)'

at line 81:

float yabs = abs(y);

编译器执行 absunqualified lookup。然后会发生什么取决于您包含的内容以及是否有 using 声明(有趣的是在编译器上)。 cmath 中的所有内容都在命名空间 std 中定义,因此您必须使用 std::.

来限定您的调用
Vector2f Vector2f::abs()
{
    //I get the error, "undefined reference to `Vector2f::abs(float)'", here
    //when trying to call abs(x) and abs(y)
    float xabs = std::abs(x); 
    float yabs = std::abs(y);
    return Vector2f(xabs, yabs);
}

如果您在 Vector2d::abs 之前的某处有 using std::absusing namespace std,您只能通过 ::abs 获得资格。 Compilers are allowed to declare C functions such as abs in the global namespace in addition to namespace std,因此根据编译器的不同,使用 ::abs 可能会在根本不使用声明的情况下工作或不工作。 Clang 3.8 接受代码,gcc 不接受。

PS:我希望函数 Vector2f::abs 计算向量范数,而不是具有原始分量绝对值的向量。不过我又不是数学家