c++矩阵计算

发布于 2024-10-19 16:49:01 字数 1492 浏览 1 评论 0原文

我创建了以下问题(目前)将计算行列式或矩阵。(我将扩展它,这就是我输入维度和矩阵的原因)。

已编辑--->>仍然存在的问题:

  • 它无法识别我的函数中的变量“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

却一份温柔 2024-10-26 16:49:01

您想要做的是将 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 take rows as a parameter and contain the mat=new int *[rows] line. Note that you'll also need to initialize each row of mat; your allocate method does this and should be called from the constructor.

时光暖心i 2024-10-26 16:49:01

mat 的初始化属于构造函数,

matrix::matrix(int _rows, int _columns)
: rows(_rows)
, columns(_columns)
, mat(new int *[rows])
{
    for (int r = 0; r < rows; ++r)
        mat[r] = new int[columns];
}

不要忘记析构函数:

matrix::~matrix()
{
    for (int r = 0; r < rows; ++r)
        delete[] mat[r];
    delete[] mat;
}

话虽这么说,我强烈建议不要发明另一个“我自己的伟大的矩阵类库”。使用有效且成熟的东西,例如 Eigen

另外,对于容器,除非你有充分的理由,否则请使用一些好的类库。例如带有 std::vector 的标准库等等,或者(在我看来更好):像 QVector 这样的 Qt 类。

The initialization of mat belongs into the constructor, say

matrix::matrix(int _rows, int _columns)
: rows(_rows)
, columns(_columns)
, mat(new int *[rows])
{
    for (int r = 0; r < rows; ++r)
        mat[r] = new int[columns];
}

Do not forget the destructor:

matrix::~matrix()
{
    for (int r = 0; r < rows; ++r)
        delete[] mat[r];
    delete[] mat;
}

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.

左耳近心 2024-10-26 16:49:01

鉴于矩阵的大小是可变的并且矩阵数据将位于堆中,因此您必须在构造函数中或通过延迟实例化来初始化矩阵数据。我之前回答过一个关于创建矩阵的问题。这是它的链接。如果您查看那里,您可以观察到在知道矩阵大小之前数据并未初始化,并且在您的情况下它是在堆内存中创建的。

编辑: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 the mat instance.

鸵鸟症 2024-10-26 16:49:01

new 运算符动态分配内存(即从空闲存储中分配)。因此,在本例中,将其返回地址分配给成员变量应该是任何成员函数的一部分,而不是类声明的一部分。它可以是 getdata() 成员函数的一部分。

而且,类需要一个析构函数,因为它正在管理资源。

编辑 1:

问题 -

  1. int **mat=new int *[rows]; 是参数化构造函数中的局部变量,一旦对构造函数的调用会引发内存泄漏。

  2. 使用语句matrix a;,将调用默认构造函数。即,matrix::matrix(),不带参数的构造函数。但是,您正在参数化构造函数中进行初始化。

为了让一切顺利,试试这个 -

class matrix
{
    int **mat;
    // ...

    public:
    matrix()
    {
        cout <<"Enter number of rows :"<<endl;
        cin >>rows;
        cout <<"Enter number of columns : "<<endl;
        cin >> columns;

        mat=new int *[rows];
        for (int i=0;i<rows;i++) {
            mat[i]=new int [columns];
        }
    }

    // And no more, getdata() member function is required. Avoid it's call in main()
};

希望它有帮助!

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 your getdata() member function.

And also, class requires a destructor because it is managing resources.

Edit 1 :

Problems -

  1. 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.

  2. 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 -

class matrix
{
    int **mat;
    // ...

    public:
    matrix()
    {
        cout <<"Enter number of rows :"<<endl;
        cin >>rows;
        cout <<"Enter number of columns : "<<endl;
        cin >> columns;

        mat=new int *[rows];
        for (int i=0;i<rows;i++) {
            mat[i]=new int [columns];
        }
    }

    // And no more, getdata() member function is required. Avoid it's call in main()
};

Hope it helps !

温折酒 2024-10-26 16:49:01

对于您的直接问题:

mat 似乎是构造函数中的局部变量,而不是类成员
ij 是在 fill() 中声明的局部变量,但不是 guidant() 中声明的局部

变量还有其他主要问题:

  • 您的矩阵未初始化。这些将具有任何随机值,并且考虑到它们是有符号整数,甚至可能是负数。您的构造函数很可能会抛出 bad_alloc

  • fill with what?

  • 这不是获取行列式的正确公式,在任何情况下都只适用于方阵

  • < p>你最好使用向量而不是数组并且只使用一个向量。请参阅 C++ 常见问题解答,了解最简单的制作矩阵的方法。

    假设您确实希望它是 int 而非 double 的矩阵,请这样声明“mat”:

    std::vector; mat;

你的构造函数应该类似于:

matrix::matrix (int r,int c) :
   rows(r), columns(c), mat(r*c)
{
    //body
}

重载操作符[] 因此:

int* operator[](int r)
{
    return &mat[r*columns];
}

const int* operator[](int r) const
{
    return &mat[r*columns];
}

注意我们有两个。可以根据其中一个来实施另一个。有些人还喜欢重载operator()来接受2个参数,即行和列,但上面的方法也可以。

您可能更喜欢使用 size_t 而不是 int 作为维度。

With your immediate question:

mat appears to be a local variable in your constructor and not a class member
i and j are local variables declared in fill() but not determinant()

There are other major issues:

  • Your matrix does not initialize rows or columns. These will have any random value and given they are signed ints, could even be negative. Your constructor might well throw bad_alloc

  • fill 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:

matrix::matrix (int r,int c) :
   rows(r), columns(c), mat(r*c)
{
    //body
}

Overload operator[] thus:

int* operator[](int r)
{
    return &mat[r*columns];
}

const int* operator[](int r) const
{
    return &mat[r*columns];
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文