我的定义有什么问题? C++

What is wrong with my definition? C++

我认为这是一个简单的错误,因为所有其他文件都在抱怨同样的错误。但是我试着把那些helper functions放在一个叫Tools的命名空间里,怎么还是报这个错呢?整个项目可以在这里找到 https://github.com/luming89/MyGameEngine。顺便说一句,如果您知道如何用 glm 替换这些助手中的任何一个,请告诉我。非常感谢!

// This is the Tools.h file
#ifndef TOOLS_H
#define TOOLS_H

#define MATH_PI 3.1415926535897932384626433832795
#define ToRadians(x) (float)(((x) * MATH_PI / 180.0f))
#define ToDegrees(x) (float)(((x) * 180.0f / MATH_PI))

typedef glm::detail::tquat<float, glm::precision::highp> Quaternion;

namespace Tools
{
    glm::mat4 initRotationFromVectors(const glm::vec3& n, const glm::vec3& v, const glm::vec3& u) // forward, up, right
    {
        glm::mat4 res; // Identity?
        res[0][0] = u.x;   res[1][0] = u.y;   res[2][0] = u.z;   res[3][0] = 0;
        res[0][1] = v.x;   res[1][1] = v.y;   res[2][1] = v.z;   res[3][1] = 0;
        res[0][2] = n.x;   res[1][2] = n.y;   res[2][2] = n.z;   res[3][2] = 0;
        res[0][3] = 0;       res[1][3] = 0;       res[2][3] = 0;       res[3][3] = 1;
        return res;
    }

    glm::vec3 transformToVec3(const glm::mat4& m, const glm::vec3& r)
    {
        glm::vec3 ret;

        for (unsigned int i = 0; i < 3; i++)
        {
            ret[i] = 0;
            for (unsigned int j = 0; j < 3; j++)
                ret[i] += m[j][i] * r[j];
        }

        return ret;
    }

    glm::vec3 GetRight(const Quaternion r)
    {
        return glm::rotate(r, glm::vec3(1, 0, 0));
    }

    glm::mat4 initTranslation(glm::mat4& m, const glm::vec3& r)
    {
        for (unsigned int i = 0; i < 4; i++)
        {
            for (unsigned int j = 0; j < 4; j++)
            {
                if (i == 3 && j != 3)
                    m[i][j] = r[j];
                else if (i == j)
                    m[i][j] = 1;
                else
                    m[i][j] = 0;
            }
        }
        m[3][3] = 1;
        return m;
    }

    glm::mat4 initScale(glm::mat4& m, const glm::vec3& r)
    {
        for (unsigned int i = 0; i < 4; i++)
        {
            for (unsigned int j = 0; j < 4; j++)
            {
                if (i == j && i != 3)
                    m[i][j] = r[i];
                else
                    m[i][j] = 0;
            }
        }

        m[3][3] = 1;

        return m;
    }

    glm::mat4 toRotationMatrix(const Quaternion& rot)
    {
        glm::vec3 forward = glm::vec3(2.0f * (rot.x * rot.z - rot.w * rot.y), 2.0f * (rot.y * rot.z + rot.w * rot.x), 1.0f - 2.0f * (rot.x * rot.x + rot.y * rot.y));
        glm::vec3 up = glm::vec3(2.0f * (rot.x*rot.y + rot.w*rot.z), 1.0f - 2.0f * (rot.x*rot.x + rot.z*rot.z), 2.0f * (rot.y*rot.z - rot.w*rot.x));
        glm::vec3 right = glm::vec3(1.0f - 2.0f * (rot.y*rot.y + rot.z*rot.z), 2.0f * (rot.x*rot.y - rot.w*rot.z), 2.0f * (rot.x*rot.z + rot.w*rot.y));

        return initRotationFromVectors(forward, up, right);
    }
}

#endif

您将定义放在头文件中,这意味着每个翻译单元(.cpp 文件)都会获得一份副本。而是将定义放在 .cpp 文件中,并将声明放在 .h 文件中。 (或者您可以使函数 inline。)