在只有一个定义的情况下就已经在错误中定义了

时间:2019-07-05 20:15:13

标签: c++ visual-c++

我在命名空间下有一些功能。它们仅在单个文件(include / math / math.h)中定义,但是由于某些原因,我在已经定义的那些函数中遇到了错误。 请注意,标头保护罩就位。

我已经多次遍历解决方案中的几乎所有代码,而且找不到任何东西,但是我想那可能只是我在匆忙中。多次尝试后找不到任何东西。

include / math / math.h

#ifndef STORMENGINE_MATH_MATH_H
#define STORMENGINE_MATH_MATH_H

namespace Math
{
    // Fast inverse square root algorithm.
    // All credits go to Greg Walsh.
    // Original algorithm is available online
    // on multiple websites, including with
    // a full explanation on the English
    // Wikipedia project by WikiMedia.
    float FastInverseSquareRoot(float flNum)
    {
        long i;
        float x2, y;
        const float threehalfs = 1.5f;

        x2 = flNum * 0.5f;
        y = flNum;
        i = *(long*)& y;
        i = 0x5f3759df - (i >> 1);
        y = *(float*)& i;
        y = y * (threehalfs - (x2 * y * y));

        return y;
    }

    // Fast inverse square root algorithm.
    // All credits go to Greg Walsh.
    // Original algorithm is available online
    // on multiple websites, including with
    // a full explanation on the English
    // Wikipedia project by WikiMedia.
    float InverseSquareRoot(float flNum, int nCount = 1)
    {
        long i;
        float x2, y;
        const float threehalfs = 1.5f;

        x2 = flNum * 0.5f;
        y = flNum;
        i = *(long*)& y;
        i = 0x5f3759df - (i >> 1);
        y = *(float*)& i;

        for (int j = 0; j < nCount; ++j)
        {
            y = y * (threehalfs - (x2 * y * y));
        }

        return y;
    }

    // Fast inverse square root algorithm.
    // All credits go to Greg Walsh.
    // Original algorithm is available online
    // on multiple websites, including with
    // a full explanation on the English
    // Wikipedia project by WikiMedia.
    float FastSquareRoot(float flNum)
    {
        long i;
        float x2, y;
        const float threehalfs = 1.5f;

        x2 = flNum * 0.5f;
        y = flNum;
        i = *(long*)& y;
        i = 0x5f3759df - (i >> 1);
        y = *(float*)& i;
        y = y * (threehalfs - (x2 * y * y));

        return (1.f / y);
    }

    // Fast inverse square root algorithm.
    // All credits go to Greg Walsh.
    // Original algorithm is available online
    // on multiple websites, including with
    // a full explanation on the English
    // Wikipedia project by WikiMedia.
    float SquareRoot(float flNum, int nCount = 1)
    {
        long i;
        float x2, y;
        const float threehalfs = 1.5f;

        x2 = flNum * 0.5f;
        y = flNum;
        i = *(long*)& y;
        i = 0x5f3759df - (i >> 1);
        y = *(float*)& i;

        for (int j = 0; j < nCount; ++j)
        {
            y = y * (threehalfs - (x2 * y * y));
        }

        return (1.f / y);
    }
};

#endif // STORMENGINE_MATH_MATH_H

vector2.h的使用细分


    FORCEINLINE float Length() const
    {
        return Math::SquareRoot(x * x + y * y, 6);
    }

    FORCEINLINE float LengthFast() const
    {
        return Math::FastSquareRoot(x * x + y * y);
    }

    FORCEINLINE float LengthSqr() const
    {
        return (x * x + y * y);
    }

// ----- Multiple irrelevant things inbetween -----

    FORCEINLINE static float Length(const Vector2& vec)
    {
        return Math::SquareRoot(vec.x * vec.x + vec.y * vec.y, 6);
    }

    FORCEINLINE static float LengthFast(const Vector2& vec)
    {
        return Math::FastSquareRoot(vec.x * vec.x + vec.y * vec.y);
    }

    FORCEINLINE static float LengthSqr(const Vector2& vec)
    {
        return (vec.x * vec.x + vec.y * vec.y);
    }

vector3.h的使用细分

    FORCEINLINE float Length() const
    {
        return Math::SquareRoot(x * x + y * y + z * z, 6);
    }

    FORCEINLINE float LengthFast() const
    {
        return Math::FastSquareRoot(x * x + y * y + z * z);
    }

    FORCEINLINE float LengthSqr() const
    {
        return (x * x + y * y + z * z);
    }

// ----- Multiple irrelevant things inbetween -----

    FORCEINLINE static float Length(const Vector3& vec)
    {
        return Math::SquareRoot(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z, 6);
    }

    FORCEINLINE static float LengthFast(const Vector3& vec)
    {
        return Math::FastSquareRoot(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
    }

    FORCEINLINE static float LengthSqr(const Vector3& vec)
    {
        return (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
    }

vector4.h的使用细分

    FORCEINLINE float Length() const
    {
        return Math::SquareRoot(x * x + y * y + z * z + w * w, 6);
    }

    FORCEINLINE float LengthFast() const
    {
        return Math::FastSquareRoot(x * x + y * y + z * z + w * w);
    }

    FORCEINLINE float LengthSqr() const
    {
        return (x * x + y * y + z * z + w * w);
    }

// ----- Multiple irrelevant things inbetween -----

    FORCEINLINE static float Length(const Vector4& vec)
    {
        return Math::SquareRoot(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w, 6);
    }

    FORCEINLINE static float LengthFast(const Vector4& vec)
    {
        return Math::FastSquareRoot(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
    }

    FORCEINLINE static float LengthSqr(const Vector4& vec)
    {
        return (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
    }

我所知道的应该没有多少错误,而是出现了这些错误:

1>vector2.obj : error LNK2005: "float __cdecl Math::FastInverseSquareRoot(float)" (?FastInverseSquareRoot@Math@@YAMM@Z) already defined in vector4.obj
1>vector2.obj : error LNK2005: "float __cdecl Math::FastSquareRoot(float)" (?FastSquareRoot@Math@@YAMM@Z) already defined in vector4.obj
1>vector2.obj : error LNK2005: "float __cdecl Math::InverseSquareRoot(float,int)" (?InverseSquareRoot@Math@@YAMMH@Z) already defined in vector4.obj
1>vector2.obj : error LNK2005: "float __cdecl Math::SquareRoot(float,int)" (?SquareRoot@Math@@YAMMH@Z) already defined in vector4.obj
1>vector3.obj : error LNK2005: "float __cdecl Math::FastInverseSquareRoot(float)" (?FastInverseSquareRoot@Math@@YAMM@Z) already defined in vector4.obj
1>vector3.obj : error LNK2005: "float __cdecl Math::FastSquareRoot(float)" (?FastSquareRoot@Math@@YAMM@Z) already defined in vector4.obj
1>vector3.obj : error LNK2005: "float __cdecl Math::InverseSquareRoot(float,int)" (?InverseSquareRoot@Math@@YAMMH@Z) already defined in vector4.obj
1>vector3.obj : error LNK2005: "float __cdecl Math::SquareRoot(float,int)" (?SquareRoot@Math@@YAMMH@Z) already defined in vector4.obj

0 个答案:

没有答案