微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Sqrt,cos,罪孽未在此范围内宣布吗?

如何解决Sqrt,cos,罪孽未在此范围内宣布吗?

我只是尝试使用sincossqrt函数,但是我不断收到“未在此范围内声明”错误消息。我查看了所有错误搜索结果,并且1.编码人员正在使用终端来编译其代码(不是我正在使用的CodeBlocks)2.我尝试使用cmath来代替,添加using namespace std并使用绝对路径。我感到沮丧。

#ifndef _MATH_H
#define _MATH_H
#include </usr/include/math.h>

#define PI 3.14159265
#define DEG_TO_RAD PI / 180.0f

struct Vector2
{
    float x;
    float y;

    Vector2(float _x = 0.0f,float _y = 0.0f)
        : x(_x),y(_y) {}

    float MagnitudeSqr()
    {
        return x*x + y*y;
    }

    float Magnitude()
    {
        return (float)sqrt(x*x + y*y);
    }

    Vector2 normalized()
    {
        float mag = Magnitude();

        return Vector2(x/ mag,y /mag);
    }
};

inline Vector2 operator +(const Vector2& lhs,const Vector2& rhs)
{
    return Vector2(lhs.x + rhs.x,lhs.y + rhs.y);
}

inline Vector2 operator -(const Vector2& lhs,const Vector2& rhs)
{
    return Vector2(lhs.x - rhs.x,lhs.y - rhs.y);
}

inline Vector2 RotateVector(Vector2& vec,float angle)
{
    float radAngle = (float)(angle*DEG_TO_RAD);

    return Vector2((float)(vec.x * cos(radAngle) - vec.y * sin(radAngle)),(float)(vec.x * sin(radAngle)) + vec.y * cos(radAngle));
}


#endif // _MATH_H

没有绝对路径

||=== Build: Debug in SDL (compiler: GNU GCC Compiler) ===|
/usr/include/c++/7/cmath|83|error: ‘::acos’ has not been declared|
/usr/include/c++/7/cmath|102|error: ‘::asin’ has not been declared|
/usr/include/c++/7/cmath|121|error: ‘::atan’ has not been declared|
/usr/include/c++/7/cmath|140|error: ‘::atan2’ has not been declared|
/usr/include/c++/7/cmath|161|error: ‘::ceil’ has not been declared|
/usr/include/c++/7/cmath|180|error: ‘::cos’ has not been declared|
/usr/include/c++/7/cmath|199|error: ‘::cosh’ has not been declared|
/usr/include/c++/7/cmath|218|error: ‘::exp’ has not been declared|
/usr/include/c++/7/cmath|237|error: ‘::fabs’ has not been declared|
/usr/include/c++/7/cmath|256|error: ‘::floor’ has not been declared|
/usr/include/c++/7/cmath|275|error: ‘::fmod’ has not been declared|
/usr/include/c++/7/cmath|296|error: ‘::frexp’ has not been declared|
/usr/include/c++/7/cmath|315|error: ‘::ldexp’ has not been declared|
/usr/include/c++/7/cmath|334|error: ‘::log’ has not been declared|
/usr/include/c++/7/cmath|353|error: ‘::log10’ has not been declared|
/usr/include/c++/7/cmath|372|error: ‘::modf’ has not been declared|
/usr/include/c++/7/cmath|384|error: ‘::pow’ has not been declared|
/usr/include/c++/7/cmath|421|error: ‘::sin’ has not been declared|
/usr/include/c++/7/cmath|440|error: ‘::sinh’ has not been declared|
/usr/include/c++/7/cmath|459|error: ‘::sqrt’ has not been declared|
/usr/include/c++/7/cmath|478|error: ‘::tan’ has not been declared|
/usr/include/c++/7/cmath|497|error: ‘::tanh’ has not been declared|
/usr/include/c++/7/cmath||In function ‘constexpr int std::fpclassify(float)’:|
/usr/include/c++/7/cmath|545|error: ‘FP_NAN’ was not declared in this scope|
/usr/include/c++/7/cmath|545|error: ‘FP_INFINITE’ was not declared in this scope|
/usr/include/c++/7/cmath|545|error: ‘FP_norMAL’ was not declared in this scope|
/usr/include/c++/7/cmath|546|error: ‘FP_SUBnorMAL’ was not declared in this scope|
/usr/include/c++/7/cmath|546|error: ‘FP_ZERO’ was not declared in this scope|
/usr/include/c++/7/cmath|546|note: suggested alternative: ‘FD_ZERO’|
/usr/include/c++/7/cmath||In function ‘constexpr int std::fpclassify(double)’:|
/usr/include/c++/7/cmath|550|error: ‘FP_NAN’ was not declared in this scope|
/usr/include/c++/7/cmath|550|error: ‘FP_INFINITE’ was not declared in this scope|
/usr/include/c++/7/cmath|550|error: ‘FP_norMAL’ was not declared in this scope|
/usr/include/c++/7/cmath|551|error: ‘FP_SUBnorMAL’ was not declared in this scope|
/usr/include/c++/7/cmath|551|error: ‘FP_ZERO’ was not declared in this scope|
/usr/include/c++/7/cmath|551|note: suggested alternative: ‘FD_ZERO’|
/usr/include/c++/7/cmath||In function ‘constexpr int std::fpclassify(long double)’:|
/usr/include/c++/7/cmath|555|error: ‘FP_NAN’ was not declared in this scope|
/usr/include/c++/7/cmath|555|error: ‘FP_INFINITE’ was not declared in this scope|
/usr/include/c++/7/cmath|555|error: ‘FP_norMAL’ was not declared in this scope|
/usr/include/c++/7/cmath|556|error: ‘FP_SUBnorMAL’ was not declared in this scope|
/usr/include/c++/7/cmath|556|error: ‘FP_ZERO’ was not declared in this scope|
/usr/include/c++/7/cmath|556|note: suggested alternative: ‘FD_ZERO’|
/usr/include/c++/7/cmath||In function ‘constexpr typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value,int>::__type std::fpclassify(_Tp)’:|
/usr/include/c++/7/cmath|564|error: ‘FP_norMAL’ was not declared in this scope|
/usr/include/c++/7/cmath|564|error: ‘FP_ZERO’ was not declared in this scope|
/usr/include/c++/7/cmath|564|note: suggested alternative: ‘FD_ZERO’|
/usr/include/c++/7/cmath|1080|error: ‘::double_t’ has not been declared|
/usr/include/c++/7/cmath|1081|error: ‘::float_t’ has not been declared|
/usr/include/c++/7/cmath|1084|error: ‘::acosh’ has not been declared|
/usr/include/c++/7/cmath|1085|error: ‘::acoshf’ has not been declared|
/usr/include/c++/7/cmath|1086|error: ‘::acoshl’ has not been declared|
/usr/include/c++/7/cmath|1088|error: ‘::asinh’ has not been declared|
/usr/include/c++/7/cmath|1089|error: ‘::asinhf’ has not been declared|
/usr/include/c++/7/cmath|1090|error: ‘::asinhl’ has not been declared|
/usr/include/c++/7/cmath|1092|error: ‘::atanh’ has not been declared|
/usr/include/c++/7/cmath|1093|error: ‘::atanhf’ has not been declared|
/usr/include/c++/7/cmath|1094|error: ‘::atanhl’ has not been declared|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build Failed: 50 error(s),0 warning(s) (0 minute(s),0 second(s)) ===|

绝对:

||=== Build: Debug in SDL (compiler: GNU GCC Compiler) ===|
/home/zues/Projects/code/SDL/MathHelper.h||In member function ‘float Vector2::Magnitude()’:|
/home/zues/Projects/code/SDL/MathHelper.h|23|error: ‘sqrt’ was not declared in this scope|
/home/zues/Projects/code/SDL/MathHelper.h|23|note: suggested alternative: ‘short’|
/home/zues/Projects/code/SDL/MathHelper.h||In function ‘Vector2 RotateVector(Vector2&,float)’:|
/home/zues/Projects/code/SDL/MathHelper.h|48|error: ‘cos’ was not declared in this scope|
/home/zues/Projects/code/SDL/MathHelper.h|48|error: ‘sin’ was not declared in this scope|
||=== Build Failed: 3 error(s),0 second(s)) ===|

解决方法

您看到的错误很可能是由于使用了保留的名称:

#define _MATH_H

也被glibc使用。

规则是避免以_开头名称(规则涉及更多,但涵盖范围广且易于记忆)。


此外,请注意,而不是:

#include </usr/include/math.h>

要包括C math.h库,只需包括:

#include <math.h>

如果要确保函数位于全局名称空间中,或者:

#include <cmath>

如果要确保它们位于std名称空间中(推荐)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。