Sqrt, cos, sin 没有在此范围内声明?
Sqrt, cos, sin was not declared in this scope?
我正尝试使用 sin
、cos
和 sqrt
函数,但我不断收到“未在此范围内声明”的错误消息。我查看了错误的所有搜索结果,并且 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 warning(s) (0 minute(s), 0 second(s)) ===|
您看到的错误可能是由于使用了保留的名称:
#define _MATH_H
也被 glibc 使用。
规则是避免以_
开头的名字(规则比较复杂,但覆盖面广,容易记忆)。
此外,请注意,而不是:
#include </usr/include/math.h>
要包含C math.h
库,你只需要包含:
#include <math.h>
如果您想确保函数在全局命名空间中或:
#include <cmath>
如果您想确保它们在 std
命名空间中(推荐)。
我正尝试使用 sin
、cos
和 sqrt
函数,但我不断收到“未在此范围内声明”的错误消息。我查看了错误的所有搜索结果,并且 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 warning(s) (0 minute(s), 0 second(s)) ===|
您看到的错误可能是由于使用了保留的名称:
#define _MATH_H
也被 glibc 使用。
规则是避免以_
开头的名字(规则比较复杂,但覆盖面广,容易记忆)。
此外,请注意,而不是:
#include </usr/include/math.h>
要包含C math.h
库,你只需要包含:
#include <math.h>
如果您想确保函数在全局命名空间中或:
#include <cmath>
如果您想确保它们在 std
命名空间中(推荐)。