gpt4 book ai didi

c++ - 模板会抛出很多错误

转载 作者:行者123 更新时间:2023-11-28 01:05:58 24 4
gpt4 key购买 nike

我正在为我的一个项目创建一个帮助程序命名空间,我希望它能够使用所有类型,如整数、 float 、 double 等。但我似乎无法正确使用模板。

无论如何,这是我当前的代码,我的编译器不会吐出有关文件本身的错误,但当我编译它时,它会吐出其他文件中的数百个错误。当我删除文件中的模板时,这些错误不存在:

#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

最佳答案

如果您为 T 传入一个 int,您的 deg/rad 转换函数将无法正常工作,因为在进行转换之前 PI 将被截断为 int。我不太明白你为什么把它放在那里。

如果您在任何地方都有 using namespace,使用像 minmax 这样的变量名会导致问题。

您的 abs 函数在 if 上缺少结束符 }。这可能会导致调用点出错。

关于c++ - 模板会抛出很多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6126227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com