c++ - 没有参数的构造函数不起作用,但有参数的构造函数起作用

标签 c++

<分区>

我正在尝试用 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 的构造函数,一个具有浮点对角线,一个没有参数。 在 orthographic(...) result 中可以正确访问其元素,但在 perspective(...) 中它无法编译(当我执行 mat4 result(); 时),说“需要结构类型而不是“mat4()””。 但是,如果我将行 mat4 result(); 更改为:

mat4 result = mat4();

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

最佳答案

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

关于c++ - 没有参数的构造函数不起作用,但有参数的构造函数起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194685/

相关文章:

c++ - 如何使用C++中的装饰器模式在一行代码中装饰对象?

c++ - CPLEX - 访问解决方案 C++ 时出错

c++ - 如何在 C++ 中获得与 Fortran 相同的实值精度(Parallel Studio XE Compiler)

c++ - 编写 std::vector 与普通数组的线程安全

c++ - 关于C++单例类的使用

c++ - 如何使用函数名作为参数?

c++ - 虚继承中的重载虚函数

c++ - 我如何强制使用的模板参数实现 C++ 中的某些接口(interface)?

c++ - 无序集 : invalid operands to binary expression ('const Play' and 'const Play' )

C++ 映射插入不良行为