操作员[] []矩阵C++
我试图创建一个运算符,使我为某个矩阵的i,j的位置和另一个“填充”矩阵的位置的位置的值,IVE试图将操作员放置在标题中,但是它无法正常工作,问题可能在其他地方,但我认为这是主要问题:
int main()
{
matriz a(3,3);
matrizQuad b(3);
for(size_t i=0;i<3;++i)
for(size_t j=0;j<3;++j){
a[i][j]=1;
b[i][j]=2;
}
matriz c=a+b;
cout << "a+b:\n" << c << "\n";
cout << "traco: " << b.traco() << "\n";
return 0;
} ```
Header:
#ifndef MATRIZES_H
#define MATRIZES_H
#include <iostream>
#include <vector>
#include <ostream>
#include <sstream>
using namespace std;
typedef vector<vector<double>> array;
class matriz
{
protected:
int iLargura, iComprimento;
array m;
public:
matriz(int L, int C): iLargura (L), iComprimento (C)
{
array m(L);
for (int i = 0; i<L;i++)
m[i].resize(C);
};
int nL() const {return iLargura;}
int nC() const {return iComprimento;}
vector<double>& operator[] (int i) {return m[i];}
const vector<double>& operator[] (int i) const {return m[i];}
};
class matrizQuad: public matriz
{
public:
matrizQuad (int T): matriz(T,T) {}
double traco();
};
matriz operator+(const matriz& m1, const matriz& m2);
ostream& operator<<(ostream& output, const matriz& m1);
#endif // MATRIZES_H
Body:
#include "matriz.h"
double matrizQuad::traco()
{
double dSoma = 0;
for (int i = 0;i<iLargura; i++)
for (int j = 0;i<iLargura; i++)
if (i==j)
dSoma = dSoma + m[i][j];
return dSoma;
}
matriz operator+(const matriz& m1, const matriz& m2)
{
int C1 = m1.nC();
int L1 = m1.nL();
matriz m(C1,L1);
for (int i = 0;i<C1; i++)
for (int j = 0;i<L1; i++)
m[i][j] = m1[i][j] + m2[i][j];
return m;
}
ostream& operator<<(ostream& output, const matriz& m1)
{
for(int i = 0; i< m1.nL();i++)
{
for (int j=0;j<m1.nC();j++)
output << m1[i][j] << " ";
output << "\n";
}
return output;
}
Im trying to create an operator that gives me the value for the position i,j of a certain matrix and another one that "fills" the matrix in the given positions, ive tried to put the operator that you can see in the header but it isnt working, the problem might be somewhere else but i think this it the main issue:
int main()
{
matriz a(3,3);
matrizQuad b(3);
for(size_t i=0;i<3;++i)
for(size_t j=0;j<3;++j){
a[i][j]=1;
b[i][j]=2;
}
matriz c=a+b;
cout << "a+b:\n" << c << "\n";
cout << "traco: " << b.traco() << "\n";
return 0;
} ```
Header:
#ifndef MATRIZES_H
#define MATRIZES_H
#include <iostream>
#include <vector>
#include <ostream>
#include <sstream>
using namespace std;
typedef vector<vector<double>> array;
class matriz
{
protected:
int iLargura, iComprimento;
array m;
public:
matriz(int L, int C): iLargura (L), iComprimento (C)
{
array m(L);
for (int i = 0; i<L;i++)
m[i].resize(C);
};
int nL() const {return iLargura;}
int nC() const {return iComprimento;}
vector<double>& operator[] (int i) {return m[i];}
const vector<double>& operator[] (int i) const {return m[i];}
};
class matrizQuad: public matriz
{
public:
matrizQuad (int T): matriz(T,T) {}
double traco();
};
matriz operator+(const matriz& m1, const matriz& m2);
ostream& operator<<(ostream& output, const matriz& m1);
#endif // MATRIZES_H
Body:
#include "matriz.h"
double matrizQuad::traco()
{
double dSoma = 0;
for (int i = 0;i<iLargura; i++)
for (int j = 0;i<iLargura; i++)
if (i==j)
dSoma = dSoma + m[i][j];
return dSoma;
}
matriz operator+(const matriz& m1, const matriz& m2)
{
int C1 = m1.nC();
int L1 = m1.nL();
matriz m(C1,L1);
for (int i = 0;i<C1; i++)
for (int j = 0;i<L1; i++)
m[i][j] = m1[i][j] + m2[i][j];
return m;
}
ostream& operator<<(ostream& output, const matriz& m1)
{
for(int i = 0; i< m1.nL();i++)
{
for (int j=0;j<m1.nC();j++)
output << m1[i][j] << " ";
output << "\n";
}
return output;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将数组调整大小固定为463035818_is_not_a_number建议您可以使您有些工作。
如果您还打印了矩阵A和B,则可以得到:
因此设置和打印矩阵效果很好。该错误是在
操作员 +
中:具体来说:
我想您复制了
i
的上一个循环,忘了重命名所有变量。Fixing the array resize as 463035818_is_not_a_number suggested gives you a somewhat working matrix.
If you also print the matrixes a and b you get:
So setting and printing matrixes works just fine. The error is in the
operator +
:specifically:
I guess you copy&pasted the previous loop for
i
and forgot to rename all the variables.您的班级有一个成员,
这个嵌套向量从未进行调整。它具有大小
0
。在构造函数中,您声明了一个相同名称的嵌套向量
,其内部向量您在构造函数中调整了大小。随后对成员内部向量元素的任何后续访问都超出了界限。
不要在构造函数中声明另一个同名的嵌套向量,而是调整成员的大小。
请注意,
数组
对std :: vector&lt; std :: vector&lt; double&gt;&gt;&gt;
的误解。而且,嵌套向量不是有效的2D数据结构。无论如何,也不清楚您的matriz
做什么,您无法从std :: vector&lt; std :: vector&lt; double&gt;&gt;&gt; 。
Your class has a member
This nested vector is never resized. It has size
0
.In the constructor you declare a nested vector of same name
Whose inner vectors you resize in the constructor. Any subsequent access to the members inner vectors elements is out of bounds.
Do not declare another nested vector of same name in the constructor, but instead resize the member.
Note that
array
is rather misleading for astd::vector<std::vector<double>>
. Moreover a nested vector is not an efficient 2d data structure. Anyhow, it is also not clear what yourmatriz
does that you cannot already get from thestd::vector<std::vector<double>>
.