c++使用指针的自定义矩阵模板

发布于 2024-12-02 19:25:43 字数 1510 浏览 1 评论 0原文

这是我最近开始实施的矩阵模板。它没有太多功能,因为正如我所说,我刚刚开始使用它。

头文件:

#ifndef MATRIXX_H
#define MATRIXX_H

#include <iostream>
using namespace std;

template <typename theType> class Matrix
{
public:
Matrix()
{
    rows = columns = 0;
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }

}

Matrix(int rows, int columns) 
{
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }
}

~Matrix()
{
    delete [] *matrixRoot;
    delete [] matrixRoot;
}

int numrows() const
{
    return rows;
}

int numcols() const
{
    return columns;
}

private:
int rows;   
int columns;    
theType **matrixRoot;

};

#endif

当我尝试运行这个测试程序时,问题就出现了:

#include "matrixx.h"
#include <iostream>

void TestingMatrix()
{
int r = 5;
int c = 4; 
Matrix<int> a(r, c);

cout << "Number of rows: " << a.numrows() << " Number of columns: " << a.numcols() << endl;
}

int main()
{
TestingMatrix();
}

如果它是正确的,我希望输出会产生如下结果:

行数:5 列数:4

相反,我得到了一些方式

行数:134515193 列数:2515748

有趣的是,每次程序运行时,列数都会改变,但行数不会改变。

显然,如果我什至无法正确初始化矩阵的实例,我将无法到达任何地方,所以我想知道我做错了什么导致了如此错误的结果。顺便说一句,我知道我的析构函数也搞砸了,因为它导致程序出现段错误(笑),但这是我计划将来自己解决的问题。如果有人能解释为什么我会得到行和列的这些数字以及如何让它返回正确的值,我真的很感激。

谢谢。 (:

Here is a matrix template I recently started implementing. It doesn't have many functions since, like I said, I just started it.

Header file:

#ifndef MATRIXX_H
#define MATRIXX_H

#include <iostream>
using namespace std;

template <typename theType> class Matrix
{
public:
Matrix()
{
    rows = columns = 0;
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }

}

Matrix(int rows, int columns) 
{
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }
}

~Matrix()
{
    delete [] *matrixRoot;
    delete [] matrixRoot;
}

int numrows() const
{
    return rows;
}

int numcols() const
{
    return columns;
}

private:
int rows;   
int columns;    
theType **matrixRoot;

};

#endif

The problem arises when I try to run this testing program:

#include "matrixx.h"
#include <iostream>

void TestingMatrix()
{
int r = 5;
int c = 4; 
Matrix<int> a(r, c);

cout << "Number of rows: " << a.numrows() << " Number of columns: " << a.numcols() << endl;
}

int main()
{
TestingMatrix();
}

Should it be correct, I'd expect the output to yield something like:

Number of rows: 5 Number of columns: 4

Instead I get something way off:

Number of rows: 134515193 Number of columns: 2515748

It's interesting to note that the number of columns changes but not the rows each time the program is run.

Obviously I won't be able to get anywhere if I can't even initialize an instance of the matrix correctly, so I'd like to know what it is I'm doing wrong that gives me such erroneous results. Btw, I'm aware that my destructor's also messed up since it causes the program to segfault (lol) but that's a problem I plan on addressing on my own in the future. I'd really just appreciate if someone could explain why I'd get those numbers for row and column and how I can get it to return the correct values.

Thank you. (:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

北方的韩爷 2024-12-09 19:25:43
Matrix<int> a(r, c);  // Invokes constructor with arguments.

您没有初始化两个参数构造函数中存在的成员变量rowscolumns。因此从他们的 getter 中获取一些垃圾值。

Matrix(int rows, int columns) : rows(rows), columns(columns)
                               // ^^^^^^^^^^^^^^^^^^^^^^^^^ Add this
{
       // ....
}
Matrix<int> a(r, c);  // Invokes constructor with arguments.

You are not initializing the member variables rows and columns present in the two argument constructor. And so getting some garbage values from their getters.

Matrix(int rows, int columns) : rows(rows), columns(columns)
                               // ^^^^^^^^^^^^^^^^^^^^^^^^^ Add this
{
       // ....
}
凉薄对峙 2024-12-09 19:25:43

您忘记在构造函数之一中设置 rowscolumns 成员变量。一种方法是使用赋值,但初始化成员变量的首选且更好的方法是使用构造函数初始化器,如下所示

Matrix(int rows, int columns) : rows(rows), columns(columns)  
{ 
    matrixRoot = new theType *[rows]; 
    for(int i = 0; i < rows; i++) 
    { 
        matrixRoot[i] = new theType[columns]; 
    } 
} 

: a(r, c); 调用 Matrix 构造函数的双参数版本。该特定版本没有初始化行和列变量。

一个好的经验法则是,如果你得到的值完全是垃圾值,那么你就忘记了将它们初始化为某个合理的值。


话虽这么说,我建议您挑选一本好的 C++ 入门书 并通读它。您收到的错误与变量的初始化有关,这是该语言的一个非常基本的方面。

You forgot to set the rows and columns member variables in one of your constructors. One way is to use assignment, but the preferred and better way to initialize member variables is to use ctor-initializers like this:

Matrix(int rows, int columns) : rows(rows), columns(columns)  
{ 
    matrixRoot = new theType *[rows]; 
    for(int i = 0; i < rows; i++) 
    { 
        matrixRoot[i] = new theType[columns]; 
    } 
} 

The declaration Matrix<int> a(r, c); calls the two-parameter version of the Matrix constructor. That particular version did not initialize the rows and columns variables.

A good rule of thumb is that if you get totally garbage values, you've forgot to initialize them to a reasonable value somewhere.


That being said, I recommend that you pick up a good introductory C++ book and read through it. The errors you're getting is related to initialization of variables, and that is a very fundamental aspect of the language.

翻了热茶 2024-12-09 19:25:43

因为您没有在传入行和列的构造函数中初始化类的 rowscolumns 成员。因此它们包含内存中的垃圾。

Matrix(int rows, int columns) 
{
    this->rows = rows;
    this->columns = columns;
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }
}  

Because you are not initializing the rows and columns members of your class in the constructor where you pass in the rows and columns. Therefor they contain garbage from the memory.

Matrix(int rows, int columns) 
{
    this->rows = rows;
    this->columns = columns;
    matrixRoot = new theType *[rows];
    for(int i = 0; i < rows; i++)
    {
        matrixRoot[i] = new theType[columns];
    }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文