所以我一直在研究一个利用2d数组和 模板以在Netbeans 8.2上打印矩阵。我只允许 编写实现文件。(即,不允许我从头文件或main中添加/删除功能。)
理想情况下,如果类对象是int类型,它将打印一个2d数组,该数组由随机整数填充;如果类对象是float类型,它将打印由随机float填充的2d数组。到目前为止,我已经成功地获得了2D数组的输出,并且结果似乎合法。但是,我不断收到此异常“ 0 [main] review3_cis17c_template_ 302 cygwin_exception :: open_stackdumpfile:将堆栈跟踪信息转储到review3_cis17c_template_.exe.stackdump”,我认为这与在Table.h中使用Delete键错误有关。
是什么引起了麻烦?我在分配内存的机制中找不到任何错误。我将发布我的Table.h,RowAray.h和main.cpp,因为它们都是一致的。
这是我的RowAray.h:
#ifndef ROWARAY_H
#define ROWARAY_H
#include<iostream>
template<class T>
class RowAray{
protected:
int size;
T *rowData;
public:
RowAray(int);
virtual ~RowAray();
int getSize()const{return size;}
T getData(int i)const{
if(i>=0&&i<size)return rowData[i];
else return 0;}
void setData(int,T);
};
#include <cstdlib>
template<class T>
RowAray<T>::RowAray(int r) {
size = r;
rowData = new T[r];
for (int i = 0; i < r; i++) {
rowData[i] = static_cast<T>(rand() % 90 + 10);
}
}
template<class T>
RowAray<T>::~RowAray() {
delete []rowData;
}
template<class T>
void RowAray<T>::setData(int i, T value) {
rowData[i] = value;
}
#endif /* ROWARAY_H */
这是我的Table.h:
#ifndef TABLE_H
#define TABLE_H
#include "RowAray.h"
template<class T>
class Table{
protected:
int szRow;
int szCol;
RowAray<T> **columns;
public:
Table(unsigned int,unsigned int);
Table(const Table<T> &);
virtual ~Table();
int getSzRow()const {return szRow;}
int getSzCol()const {return szCol;}
T getData(int,int)const;
void setData(int,int,T);
Table<T> operator+(const Table<T> &);
};
template<class T>
Table<T>::Table(unsigned int r, unsigned int c) {
szRow = r;
szCol = c;
columns = new RowAray<T>*[r];
for(int i = 0; i < r; i++) {
columns[i] = new RowAray<T>(c);
}
}
template<class T>
Table<T>::Table(const Table<T> &t) {
szRow = t.getSzRow();
szCol = t.getSzCol();
columns = new RowAray<T>*[t.getSzRow()];
for(int i = 0; i < t.getSzRow(); i++) {
columns[i] = new RowAray<T>(t.getSzCol());
}
for (int i = 0; i < t.getSzRow(); i++) {
for (int j = 0; j < t.getSzCol(); j++) {
columns[i]->setData(j, t.getData(i, j));
}
}
}
template<class T>
T Table<T>::getData(int r, int c) const {
return columns[r]->getData(c);
}
template<class T>
Table<T>::~Table() {
for (int i = 0; i < szRow; i++) {
delete []columns;
}
delete columns;
}
template<class T>
Table<T> Table<T>::operator+(const Table<T> &t) {
Table<T> tab(t.getSzRow(), t.getSzCol());
for(int i = 0; i < t.getSzRow(); i++) {
for (int j = 0; j <t.getSzCol(); j++) {
tab.columns[i]->setData(j, this->getData(i,j) + t.getData(i,j));
}
}
return tab;
}
#endif /* TABLE_H */
最后,我的main.cpp:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
//User Libraries
#include "Table.h"
//Global Constants
//Function Prototype
template<class T>
void prntRow(RowAray<T> *,int);
template<class T>
void prntTab(const Table<T> &);
//Execution Begins Here!
int main(int argc, char** argv) {
//Initialize the random seed
srand(static_cast<unsigned int>(time(0)));
//Declare Variables
int rows=3,cols=4;
//Test out the Row with integers and floats
RowAray<int> a(3);RowAray<float> b(4);
cout<<"Test the Integer Row "<<endl;
prntRow(&a,3);
cout<<"Test the Float Row "<<endl;
prntRow(&b,4);
//Test out the Table with a float
Table<float> tab1(rows,cols);
Table<float> tab2(tab1);
Table<float> tab3=tab1+tab2;
cout<<"Float Table 1 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab1);
cout<<"Float Table 2 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab2);
cout<<"Float Table 3 size is [row,col] = Table 1 + Table 2 ["
<<rows<<","<<cols<<"]";
prntTab(tab3);
//Exit Stage Right
return 0;
}
template<class T>
void prntRow(RowAray<T> *a,int perLine){
cout<<fixed<<setprecision(1)<<showpoint<<endl;
for(int i=0;i<a->getSize();i++){
cout<<a->getData(i)<<" ";
if(i%perLine==(perLine-1))cout<<endl;
}
cout<<endl;
}
template<class T>
void prntTab(const Table<T> &a){
cout<<fixed<<setprecision(1)<<showpoint<<endl;
for(int row=0;row<a.getSzRow();row++){
for(int col=0;col<a.getSzCol();col++){
cout<<setw(8)<<a.getData(row,col);
}
cout<<endl;
}
cout<<endl;
}
这是我的第一次发布,因此,如果代码混乱,我深表歉意。任何帮助将不胜感激!