c++使用指针的自定义矩阵模板
这是我最近开始实施的矩阵模板。它没有太多功能,因为正如我所说,我刚刚开始使用它。
头文件:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有初始化两个参数构造函数中存在的成员变量
rows
和columns
。因此从他们的 getter 中获取一些垃圾值。You are not initializing the member variables
rows
andcolumns
present in the two argument constructor. And so getting some garbage values from their getters.您忘记在构造函数之一中设置
rows
和columns
成员变量。一种方法是使用赋值,但初始化成员变量的首选且更好的方法是使用构造函数初始化器,如下所示: a(r, c); 调用
Matrix
构造函数的双参数版本。该特定版本没有初始化行和列变量。一个好的经验法则是,如果你得到的值完全是垃圾值,那么你就忘记了将它们初始化为某个合理的值。
话虽这么说,我建议您挑选一本好的 C++ 入门书 并通读它。您收到的错误与变量的初始化有关,这是该语言的一个非常基本的方面。
You forgot to set the
rows
andcolumns
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 usector-initializer
s like this:The declaration
Matrix<int> a(r, c);
calls the two-parameter version of theMatrix
constructor. That particular version did not initialize therows
andcolumns
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.
因为您没有在传入行和列的构造函数中初始化类的
rows
和columns
成员。因此它们包含内存中的垃圾。Because you are not initializing the
rows
andcolumns
members of your class in the constructor where you pass in the rows and columns. Therefor they contain garbage from the memory.