错误:'float'之前的预期primary-expression

时间:2017-05-06 00:18:50

标签: c++ templates matrix vector operator-overloading

我的两个函数有一些奇怪的行为。在构建过程中调用它们时都会出错。我不确定我错过了什么,因为一切看起来都不错,但事实显然不是这样。

第一行出现错误:

matf4x4 perspective = perspective(m_fov, m_aspect, m_near, m_far); // ERROR (1)

透视函数定义:

static mat4x4<float> perspective(float fov, float aspect, float n, float f) {
    float q = 1.0f / tanf(radians(0.5f * fov));
    float A = q / aspect;
    float B = (n + f) / (n - f);
    float C = (2.0f * n * f) / (n - f);

    mat4x4<float> result;

    result[0] = vec4f(A, 0.0f, 0.0f, 0.0f);
    result[1] = vec4f(0.0f, q, 0.0f, 0.0f);
    result[2] = vec4f(0.0f, 0.0f, B, -1.0f);
    result[3] = vec4f(0.0f, 0.0f, C, 0.0f);

    return result;
}

错误(1)输出:

C:\Users\Matt\CLionProjects\SkyGames\Engine\Camera\Camera.cpp:18:66: error: no match for call to '(sky::matf4x4 {aka sky::mat4x4<float>}) (float&, float&, float&, float&)'
  matf4x4 perspective = perspective(m_fov, m_aspect, m_near, m_far);

第二行有错误:

matf4x4 lookat = lookat<float>(m_position, centre, m_up); // ERROR (2)

Lookat函数定义:

template <typename T>
static mat4x4<T> lookat(const vec3<T>& eye, const vec3<T>& centre, const vec3<T>& up) {
    vec3<T> f = normalize(centre - eye);
    vec3<T> upN = normalize(up);
    vec3<T> s = cross(f, upN);
    vec3<T> u = cross(s, f);
    mat4x4<T> M = mat4x4<T>(vec4<T>(s[0], u[0], -f[0], T(0)),
                            vec4<T>(s[1], u[1], -f[1], T(0)),
                            vec4<T>(s[2], u[2], -f[2], T(0)),
                            vec4<T>(T(0), T(0), T(0), T(1)));
    return M * translate<T>(-eye);
}

Matrix 4x4 float with operator overload =

mat4x4<T>&operator=(const mat4x4<T>& mat) {
    for (int i = 0; i < m_width; ++i)
        matrix[i] = mat.matrix[i];
    return *this;
}

错误(2)输出:

C:\Users\Matt\CLionProjects\SkyGames\Engine\Camera\Camera.cpp:20:26: error: expected primary-expression before 'float'
  matf4x4 lookat = lookat<float>(m_position, centre, m_up);

1 个答案:

答案 0 :(得分:1)

更改变量名称或功能名称。

在语句matf4x4 lookat = lookat<float>(...);中,编译器首先将变量lookat添加到其符号表中,因此外层函数lookat隐藏在当前范围内。当遇到第二个lookat时,编译器认为它是变量lookat