Search code examples
c++sdlcodeblocks

Sqrt, cos, sin was not declared in this scope?


I am just trying to use the sin, cos, and sqrt functions, but I keep getting "not declared in this scope" error messages. I've looked at all the search results for the error, and 1. the coders are using the terminal to compile their code (not CodeBlocks which is what I'm using) 2. I've tried using cmath instead, adding using namespace std, and using absolute paths. I am getting frustrated.

#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

Without absolute path

||=== 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)) ===|

With absolute:

||=== 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 warning(s) (0 minute(s), 0 second(s)) ===|

Solution

  • The errors you are seeing are likely due to using the reserved name:

    #define _MATH_H
    

    which is also used by glibc.

    The rule is to avoid starting names with _ (the rules are more involved, but that covers a lot and is easy to remember).


    In addition, note that instead of:

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

    To include C math.h library, you only need to include:

    #include <math.h>
    

    if you want to ensure the functions are in the global namespace or:

    #include <cmath>
    

    if you want to ensure they are in the std namespace (recommended).