在我添加 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/