c++ - 多维数组类错误

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

在我添加 rot() 函数之前,这段代码工作正常。是的,它在头文件中正确声明。我用简单的 1.0f 值替换了所有方程式,但出现了同样的错误。这向我暗示它与声明 Matrix2f rot 有关; ...有人知道这里的问题是什么吗?

#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”方法中不需要“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 ) 提供覆盖运算符 [] 的实现

关于c++ - 多维数组类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24809272/

相关文章:

c++ - 堆栈上的 G++ SSE 内存对齐

c++ - 将值从本地堆栈移动到堆? (C++)

c++ - 如何使使用 AUTOUIC 添加的包含目录可用于下游目标?

c - 将用逗号分隔的文本文件读取到 C 中的二维数组中

javascript - 需要帮助对特定索引中的混合字符串和整数的嵌套数组进行排序

c - 初始化使指针来自整数而不进行强制转换

ant - 如何在不使用 IDE(Netbeans 等)的情况下使用外部库编译 java 文件?

c++ - 遇到 char* 数组的问题

vb.net - “文本”不明确-错误(vb.net)

java - 对数组中的字符串和 double 类型进行排序