【问题标题】:constructor without parameters doesnt work but one with does work [duplicate]没有参数的构造函数不起作用,但有参数的构造函数起作用[重复]
【发布时间】:2015-07-23 13:15:01
【问题描述】:

我正在尝试用 C++ 编写矩阵结构,我是新手。 mat4.h 文件:

#include "math.h"
namespace engine {
    namespace math {
        struct mat4 {
            float elements[4 * 4]; // column major ordering, index = row + col * 4

            mat4();
            mat4(float diagonal);


            mat4& mul(const mat4& other);
            static mat4 identity(); // construct and return an identity matrix
            static mat4 orthographic(float left, float right, float bottom, float top, float near, float far); // boundaries (clipping planes)
            static mat4 perspective(float fov, float aspectRatio, float near, float far);
            static mat4 translation(const vec3& translation);
            static mat4 rotation(float angle, const vec3 & axis);
            static mat4 scale(const vec3 & scale);
            friend mat4 operator*(mat4 left, const mat4 & right);
            mat4& operator*=(const mat4 &other); 

        };
    }
}

这里是 mat4.cpp:

#include "mat4.h"
namespace engine {
    namespace math {

        mat4::mat4() {
            for (int i = 0; i < 4 * 4; i++)
                elements[i] = 0;
        }
        mat4::mat4(float diagonal) {
            for (int i = 0; i < 4 * 4; ++i) {
                elements[i] = 0;
            }
            for(int i = 0; i < 4; i += 1)
                elements[i + i * 4] = diagonal;
        }
        mat4& mat4::mul(const mat4 &other) {
            for (int i = 0; i < 4; ++i)        // col
                for (int j = 0; j < 4; ++j) {  // row
                    float sum = 0;
                    for (int k = 0; k < 4; ++k)
                        sum += elements[j + k * 4] * other.elements[k + i * 4];
                    elements[j + i * 4] = sum;
                }
        }

        mat4 mat4::identity() {
            return mat4(1);
        }

        mat4 operator*(mat4 left, const mat4 &right) {
            return left.mul(right);
        }

        mat4 &mat4::operator*=(const mat4 &other) {
            return mul(other);
        }

        mat4 mat4::orthographic(float left, float right, float bottom, float top, float near, float far) {
            mat4 result(1);
            result.elements[0 + 0 * 4] =  2.0f / (right - left);
            result.elements[1 + 1 * 4] =  2.0f / (top - bottom);
            result.elements[2 + 2 * 4] = -2.0f / (far - near);
            result.elements[0 + 3 * 4] = (left + right) / (left - right);
            result.elements[1 + 3 * 4] = (bottom + top) / (bottom - top);
            result.elements[2 + 3 * 4] = (far + near)   / (far - near);
            //result.elements[3 + 3 * 4] = 1; this is achieved by mat result(1);
            return result;
        }

        mat4 mat4::perspective(float fov, float aspectRatio, float near, float far) {
            mat4 result();

            float q = 1.0f / (float) tan(toRadians(fov) / 2.0f);
            result.elements[0 + 0 * 4] = q / aspectRatio;
            result.elements[1 + 1 * 4] = q;
            result.elements[2 + 2 * 4] = (-near - far)  / (near - far);
            result.elements[3 + 2 * 4] = 1;
            result.elements[2 + 3 * 4] = 2 * far * near / (near - far);
            return result;
        }
    }
}

如您所见,我有 2 个 mat4 构造函数,一个得到浮点对角线,一个没有参数。 在正交(...)结果可以正确访问其元素,但在透视(...)它不编译(当我做mat4 result();),说“需要结构类型而不是'mat4()'”。 但是,如果我将mat4 result(); 行更改为:

mat4 result = mat4();

然后一切都很好。 这是怎么回事?

【问题讨论】:

    标签: c++


    【解决方案1】:

    mat4 result(); 声明一个函数。将其更改为 mat4 result; 以声明(并默认初始化)一个变量。

    【讨论】:

    • 所以如果我需要一个参数,我使用mat4 result(5),否则,mat4 result?
    • @shoham:是的,没错。
    • 我从标题中猜到了解决方案。这是一个太常见的陷阱,有太多相同的答案。
    • @nwp 对不起,我只是不知道去哪里看/搜索什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多