【问题标题】:Multi-Dimensional Array class error多维数组类错误
【发布时间】:2014-09-08 15:54:55
【问题描述】:

在我添加 rot() 函数之前,这段代码运行良好。是的,它在头文件中正确声明。我用简单的 1.0f 值替换了所有方程,但发生了同样的错误。这向我暗示它与宣布 Matrix2f 腐烂有关。 ...有没有人知道这里的问题是什么?

#include "Matrix2f.h"
#include <cmath>
#include <iostream>
#include "Vector2f.h"

Matrix2f::Matrix2f(){

    m[0][0]= 1.0f;    m[0][1]= 0.0f;
    m[1][0]= 0.0f;    m[1][1]= 1.0f;

}

Vector2f Matrix2f::rot(float theta, Vector2f vec){

     Matrix2f rot;

        rot[0][0]= cosf(theta);  rot[0][1]= -sinf(theta);
        rot[1][0]= sinf(theta);  rot[1][1]= cosf(theta);

        float tx = ((rot[0][0]*vec.getX())+(rot[0][1]*vec.getY()));
        float ty = ((rot[1][0]*vec.getX())+(rot[1][1]*vec.getY()));


    return Vector2f(tx, ty);

}

void Matrix2f::printMat(){

    std::cout << "| " << m[0][0] << "    " << m[0][1] << " |" << std::endl;
    std::cout << "| " << m[1][0] << "    " << m[1][1] << " |" << std::endl;
}

编译器给出的错误:

|17|error: no match for 'operator[]' in 'rot[0]'|

它为从第 17 行到第 21 行的每一行给出两次相同的代码... 非常感谢任何帮助:)

【问题讨论】:

  • 看起来是 rot.m[0][0] ?
  • 向我们展示operator[]Matrix2f 中的重载。这肯定是问题所在。

标签: c++ multidimensional-array compiler-errors operators


【解决方案1】:

首先,“rot”方法中不需要“Matrix2f rot”对象。

您可以将方法更改为:

Vector2f Matrix2f::rot(float theta, Vector2f vec){

    float tx = (( cosf(theta) * vec.getX())+( ( -sinf(theta) ) * vec.getY()));
    float ty = (( sinf(theta) * vec.getX())+( cosf(theta) * vec.getY()));

    return Vector2f(tx, ty);
}

除非您想在“rot”中重置成员变量“m”(我假设为“float m[2][2]”) 那么你可以使用:

Vector2f Matrix2f::rot(float theta, Vector2f vec){

    m[0][0]= cosf(theta);  m[0][1]= -sinf(theta);
    m[1][0]= sinf(theta);  m[1][1]= cosf(theta);

    float tx = (( m[0][0] * vec.getX())+( m[0][1] ) * vec.getY()));
    float ty = (( m[1][0] * vec.getX())+( m[1][1] * vec.getY()));

    return Vector2f(tx, ty);

}

你不能使用 rot[][] 除非你的类 ( Matrix2f ) 提供了覆盖操作符 [] 的实现

【讨论】:

  • 谢谢,看来我把整个过程复杂化了。
【解决方案2】:

您需要访问 m 对象内部的多维数组。为此,请使用 rot.m 作为多维数组。

【讨论】:

  • 但是构造函数应该自己处理。在这种情况下,任何声明的矩阵都会初始化为单位矩阵。我不是在创建多维数组的多维数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 2015-08-17
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2021-03-12
相关资源
最近更新 更多