Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Sqrt,cos,sin未在此范围内声明?_C++_Sdl_Codeblocks - Fatal编程技术网

C++ Sqrt,cos,sin未在此范围内声明?

C++ Sqrt,cos,sin未在此范围内声明?,c++,sdl,codeblocks,C++,Sdl,Codeblocks,我只是尝试使用sin、cos和sqrt函数,但我一直收到“未在此范围内声明”错误消息。我已经查看了所有错误的搜索结果,结果是1。编码人员正在使用终端来编译他们的代码(不是我正在使用的代码块)。我尝试使用cmath,使用命名空间std添加,并使用绝对路径。我越来越沮丧了 #ifndef _MATH_H #define _MATH_H #include </usr/include/math.h> #define PI 3.14159265 #define DEG_TO_RAD PI /

我只是尝试使用
sin
cos
sqrt
函数,但我一直收到“未在此范围内声明”错误消息。我已经查看了所有错误的搜索结果,结果是1。编码人员正在使用终端来编译他们的代码(不是我正在使用的代码块)。我尝试使用
cmath
,使用命名空间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

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

glibc也使用该方法

规则是避免名字以
开头(规则更复杂,但涉及面更广,也更容易记住)


此外,请注意,而不是:

#include </usr/include/math.h>
如果要确保函数位于全局命名空间中,或者:

#include <cmath>
#包括

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

请将Q&A格式保持为on。解决方案应该出现在答题帖中,而不是问题帖中,或者只使用
#pragma once
来防止多重包含
#define _MATH_H
#include </usr/include/math.h>
#include <math.h>
#include <cmath>