多维数组类错误

时间:2014-07-17 16:50:06

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

此代码正常工作,直到我添加了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行的每一行提供两次相同的代码... 任何帮助非常感谢:)

2 个答案:

答案 0 :(得分:2)

首先,在“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)提供覆盖运算符的实现[]

答案 1 :(得分:0)

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

相关问题