开发者

Templating throws out a lot of errors

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


Your deg/rad conversion functions won't work right if you pass in an int for T 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 and max will cause problems if you have a using namespace anywhere.

Your abs function is missing a closing } on the if. That could cause errors at the call point.


A curly brace is missed in if.

if(value < 0) { value = -value;  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜