c++矩阵计算
我创建了以下问题(目前)将计算行列式或矩阵。(我将扩展它,这就是我输入维度和矩阵的原因)。
已编辑--->>仍然存在的问题:
- 它无法识别我的函数中的变量“mat”。
错误:“mat”未在此范围内声明
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
class matrix {
private:
int rows,columns;
public:
//constructor
matrix ()
{}
matrix(int _rows,int _columns) : rows(_rows),columns(_columns){
{
int **mat=new int *[rows];
for (int r = 0; r < rows; ++r)
mat[r] = new int[columns];
}
}
//destructor
~matrix(){
for (int r = 0; r < rows; ++r)
delete[] mat[r];
delete[] mat;
}
//void allocate();
void getdata() {
cout <<"Enter number of rows :"<<endl;
cin >>rows;
cout <<"Enter number of columns : "<<endl;
cin >> columns;
}
void fill();
double determinant();
void showdata(){
}
};
int main()
{
matrix a;
a.getdata();
a.fill();
a.determinant();
return 0;
}
//fills the matrix
void matrix ::fill(){
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++){
cout <<"Enter the elements in a line separated by whitespace :"<<endl;
cin >>mat[i][j];
}
}
}
//calculate the determinant
double matrix :: determinant (){
double det;
det = mat[rows][columns]*mat[rows+1][columns+1] - mat[rows][columns+1]*mat[rows+1][columns];
cout <<"The determinant of matrix is :"<<det<<endl;}
I have created the following problem which (for now) is going to calculate the determinant or a matrix.( I will expand it that's why I input the dimensions and the matrices).
EDITED--->>> The problem which remains :
- It doesn't recognize the variables "mat " in my functions.
error: ‘mat’ was not declared in this scope
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
class matrix {
private:
int rows,columns;
public:
//constructor
matrix ()
{}
matrix(int _rows,int _columns) : rows(_rows),columns(_columns){
{
int **mat=new int *[rows];
for (int r = 0; r < rows; ++r)
mat[r] = new int[columns];
}
}
//destructor
~matrix(){
for (int r = 0; r < rows; ++r)
delete[] mat[r];
delete[] mat;
}
//void allocate();
void getdata() {
cout <<"Enter number of rows :"<<endl;
cin >>rows;
cout <<"Enter number of columns : "<<endl;
cin >> columns;
}
void fill();
double determinant();
void showdata(){
}
};
int main()
{
matrix a;
a.getdata();
a.fill();
a.determinant();
return 0;
}
//fills the matrix
void matrix ::fill(){
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++){
cout <<"Enter the elements in a line separated by whitespace :"<<endl;
cin >>mat[i][j];
}
}
}
//calculate the determinant
double matrix :: determinant (){
double det;
det = mat[rows][columns]*mat[rows+1][columns+1] - mat[rows][columns+1]*mat[rows+1][columns];
cout <<"The determinant of matrix is :"<<det<<endl;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您想要做的是将
mat
初始化为动态大小的数组。正如编译器告诉您的那样,这不能在常量表达式中完成。您必须为
matrix
类编写一个构造函数,它将采用rows
作为参数并包含mat=new int *[rows]
行。请注意,您还需要初始化mat
的每一行;您的allocate
方法执行此操作,并且应该从构造函数中调用。What you are trying to do is initialize
mat
to be an array of dynamic size. This cannot be done in, as your compiler tells you, a constant expression.You must write a constructor for the
matrix
class which will takerows
as a parameter and contain themat=new int *[rows]
line. Note that you'll also need to initialize each row ofmat
; yourallocate
method does this and should be called from the constructor.mat 的初始化属于构造函数,
不要忘记析构函数:
话虽这么说,我强烈建议不要发明另一个“我自己的伟大的矩阵类库”。使用有效且成熟的东西,例如 Eigen。
另外,对于容器,除非你有充分的理由,否则请使用一些好的类库。例如带有
std::vector
的标准库等等,或者(在我看来更好):像 QVector 这样的 Qt 类。The initialization of
mat
belongs into the constructor, sayDo not forget the destructor:
This being said, I highly advice against inventing yet another "my great own matrix class library". Use something efficient and established such as Eigen.
Also, for containers, unless you have really good reasons, use some good class library for that. E.g. the standard library with
std::vector
and so on, or (even better in my taste): Qt classes like QVector.鉴于矩阵的大小是可变的并且矩阵数据将位于堆中,因此您必须在构造函数中或通过延迟实例化来初始化矩阵数据。我之前回答过一个关于创建矩阵的问题。这是它的链接。如果您查看那里,您可以观察到在知道矩阵大小之前数据并未初始化,并且在您的情况下它是在堆内存中创建的。
编辑:C++不允许在声明成员变量时初始化堆中的成员变量。在您的情况下,矩阵的大小首先是未知的,以便使用正确大小的内存创建
mat
。正如这里每个人都提到的,使用构造函数创建mat
实例。Seeing that you've variable size for your matrix and that your matrix data is going to be in the heap, you've to initialize your matrix data in the constructor or by lazy instantiation. I had previously answered a question for creating a matrix. Here's the link to it. If you look in there you can observe that data is not initialised before the size of the matrix is known and as in your case it's created in the heap memory.
EDIT: C++ does not allow initialisation of member variables in the heap when they are declared. And in your case the size of the matrix is not known in the first place for
mat
to be created with the right size of memory. As mentioned by everybody in here, use the constructor to create themat
instance.new
运算符动态分配内存(即从空闲存储中分配)。因此,在本例中,将其返回地址分配给成员变量应该是任何成员函数的一部分,而不是类声明的一部分。它可以是getdata()
成员函数的一部分。而且,类需要一个析构函数,因为它正在管理资源。
编辑 1:
问题 -
int **mat=new int *[rows];
是参数化构造函数中的局部变量,一旦对构造函数的调用会引发内存泄漏。使用语句
matrix a;
,将调用默认构造函数。即,matrix::matrix()
,不带参数的构造函数。但是,您正在参数化构造函数中进行初始化。为了让一切顺利,试试这个 -
希望它有帮助!
new
operator dynamically allocates memory ( i.e., from the free store ). So, it should be part of any member function to assign its return address to a member variable in this example and not the part of class declaration. It can be a part of yourgetdata()
member function.And also, class requires a destructor because it is managing resources.
Edit 1 :
Problems -
int **mat=new int *[rows];
, is a local variable in the parameterized constructor and goes out of scope once the call to the constructor is done raising memory leaks.With the statement,
matrix a;
, the default constructor will be called. i.e.,matrix::matrix()
, the constructor with no arguments. However, you are making your initialization in the parameterized constructor.To make everything work, try this -
Hope it helps !
对于您的直接问题:
mat
似乎是构造函数中的局部变量,而不是类成员i
和j
是在fill()
中声明的局部变量,但不是guidant()
中声明的局部变量还有其他主要问题:
您的矩阵未初始化
行
或列
。这些将具有任何随机值,并且考虑到它们是有符号整数,甚至可能是负数。您的构造函数很可能会抛出 bad_allocfill with what?
这不是获取行列式的正确公式,在任何情况下都只适用于方阵
假设您确实希望它是 int 而非 double 的矩阵,请这样声明“mat”:
std::vector; mat;
你的构造函数应该类似于:
重载操作符[] 因此:
注意我们有两个。可以根据其中一个来实施另一个。有些人还喜欢重载operator()来接受2个参数,即行和列,但上面的方法也可以。
您可能更喜欢使用 size_t 而不是 int 作为维度。
With your immediate question:
mat
appears to be a local variable in your constructor and not a class memberi
andj
are local variables declared infill()
but notdeterminant()
There are other major issues:
Your matrix does not initialize
rows
orcolumns
. These will have any random value and given they are signed ints, could even be negative. Your constructor might well throw bad_allocfill with what?
That isn't the correct formula to get a determinant, which in any case only applies to square matrices
You would be better off using vector rather than arrays and only one vector. Look at the C++ FAQ to see the simplest way to make a matrix.
Assuming you do want it to be a matrix of int not double, declare "mat" this way:
std::vector<int> mat;
Your constructor should be something like:
Overload operator[] thus:
Note we have 2 of them. It is possible to implement one in terms of the other. Some also like to overload operator() to take 2 parameters, row and column, but the above will work.
You might prefer to use size_t rather than int for dimensions.