模板化会引发很多错误
我正在为我的一个项目创建一个辅助命名空间,我希望它能够使用所有类型,如整数、浮点数、双精度数等。但似乎我无法正确使用模板。
无论如何,这是我当前的代码,我的编译器不会吐出有关文件本身的错误,尽管当我编译它时,它会吐出其他文件中的数百个错误。 当我删除文件中的模板时,这些错误不存在:
#include "..\util\Logger.hpp"
namespace gm
{
namespace math
{
namespace MathHelper
{
// Value of Pi
const double PI = 3.1415926535897932384626433832795028841972;
// Value of euler
const double E = 2.7182818284590452353602874713526624977572;
// Convert radians to degrees
template <typename T>
T Rad2Deg(T angle)
{
return angle * (180 / (T)PI);
}
// Convert degrees to radians
template <typename T>
T Deg2Rad(T angle)
{
return angle * ((T)PI / 180);
}
// Clamp a value in between the given min and max
template <typename T>
T Clamp(T value, T min, T max)
{
if(min > max) { gm::util::Logger::DisplayError("Invalid argument in MathHelper::Clamp, max is over min"); }
if(value < min) { value = min; }
if(value > max) { value = max; }
return value;
}
// Exponentiate value a with value b
template <typename T>
T Exp(T a, int b)
{
if(b < 0) { gm::util::Logger::DisplayError("Invalid argument in MathHelper::Exp, b must be positive"); }
T value = a;
for(int i = 1; i < b; i++) { value *= a; }
return value;
}
// Get the absolute value of the value passed
template <typename T>
T Abs(T a, T b)
{
if(value < 0) { value = -value;
return value;
}
};
};
};
我将编译错误放入此粘贴中: http://pastebin .com/AxwmDyDh
I am creating a helper namespace for one of my projects, i wanted it to be able to use all types like ints, floats, doubles etc. But it seems like i just cant get the templating right.
Anyways, here is my current code, my compiler doesn't spit out errors about the file itself, though when i compile it it spits out hundreds of errors in other files.
These errors aren't there when i remove the templating in the file:
#include "..\util\Logger.hpp"
namespace gm
{
namespace math
{
namespace MathHelper
{
// Value of Pi
const double PI = 3.1415926535897932384626433832795028841972;
// Value of euler
const double E = 2.7182818284590452353602874713526624977572;
// Convert radians to degrees
template <typename T>
T Rad2Deg(T angle)
{
return angle * (180 / (T)PI);
}
// Convert degrees to radians
template <typename T>
T Deg2Rad(T angle)
{
return angle * ((T)PI / 180);
}
// Clamp a value in between the given min and max
template <typename T>
T Clamp(T value, T min, T max)
{
if(min > max) { gm::util::Logger::DisplayError("Invalid argument in MathHelper::Clamp, max is over min"); }
if(value < min) { value = min; }
if(value > max) { value = max; }
return value;
}
// Exponentiate value a with value b
template <typename T>
T Exp(T a, int b)
{
if(b < 0) { gm::util::Logger::DisplayError("Invalid argument in MathHelper::Exp, b must be positive"); }
T value = a;
for(int i = 1; i < b; i++) { value *= a; }
return value;
}
// Get the absolute value of the value passed
template <typename T>
T Abs(T a, T b)
{
if(value < 0) { value = -value;
return value;
}
};
};
};
I put the compile errors in this paste: http://pastebin.com/AxwmDyDh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
if
中缺少大括号。A curly brace is missed in
if
.如果您为
T
传递一个int
,您的 deg/rad 转换函数将无法正常工作,因为 PI 在进行转换之前会被截断为 int。我不太明白你为什么把它放在那里。如果您在任何地方都有
using namespace
,那么使用min
和max
之类的变量名称会导致问题。您的
abs
函数在if
上缺少结束}
。这可能会导致调用点出现错误。Your deg/rad conversion functions won't work right if you pass in an
int
forT
because PI will get truncated to int before doing the conversion. I can't quite make out why you have that in there.Using variable names like
min
andmax
will cause problems if you have ausing namespace
anywhere.Your
abs
function is missing a closing}
on theif
. That could cause errors at the call point.