我面临一个二维指针数组的问题。它编译时没有错误,但是当我尝试运行文件时,我得到的只是一行,说我有分段错误。
我的头文件:
#ifndef __TWODARRAY_H__
#define __TWODARRAY_H__
template <typename T>
class TwoDArray {
private:
T** theArray;
int numRows;
int numCols;
T defSpace;
public:
TwoDArray<T> (int r, int c, T def);
~TwoDArray<T>();
void insert(int r, int c, T value);
T access(int r, int c);
void remove(int r, int c);
void print();
int getNumRows();
int getNumCols();
};
#endif
我的方法:
#include "TwoDArray.h"
#include <iostream>
#include <assert.h>
#include <string>
//initializes the 2D Array
template <typename T>
TwoDArray<T>::TwoDArray(int r, int c, T def) {
assert(r > 0 && c > 0);
numRows = r;
numCols = c;
defSpace = def;
theArray = new T*[r];
for(int i=0; i<r; i++) {
theArray[i] = new T[c];
}
//sets all values to the default
for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) {
theArray[i][j] = defSpace;
}
}
}
//deletes the 2D Array
template<typename T>
TwoDArray<T>::~TwoDArray() {
for(int i=0; i<numRows; i++) {
delete[] theArray[i];
}
delete[] theArray;
}
//inserts value v at row r and column c
template<typename T>
void TwoDArray<T>::insert(int r, int c, T value) {
assert(r < numRows && c < numCols);
assert(value != defSpace);
theArray[r][c] = value;
}
//get value at row r, column c
template<typename T>
T TwoDArray<T>::access(int r, int c) {
assert(r < numRows && c < numCols);
T result = theArray[r][c];
return result;
}
//set value at row r and column c back to default
template<typename T>
void TwoDArray<T>::remove(int r, int c) {
assert(r < numRows && c < numCols);
assert(theArray[r][c] != defSpace);
theArray[r][c] = defSpace;
}
//print the 2D Array
template<typename T>
void TwoDArray<T>::print() {
for(int i=0; i<numRows; i++) {
for(int j=0;j<numCols; i++) {
std::cout << theArray[i][j];
std::cout << " ";
}
std::cout << std::endl;
}
}
//gets number of rows for test
template<typename T>
int TwoDArray<T>::getNumRows() {
return numRows;
}
//gets number of columns for test
template<typename T>
int TwoDArray<T>::getNumCols() {
return numCols;
}
template class TwoDArray<int>;
template class TwoDArray<std::string>;
而我的主要:
#include <iostream>
#include <string>
#include "TwoDArray.h"
using std::cout;
using std::endl;
int main() {
TwoDArray<int>* i = new TwoDArray<int>(5, 5, 0);
TwoDArray<std::string>* s = new TwoDArray<std::string>(5, 5, "o");
i->insert(1, 1, 1);
i->insert(1, 3, 1);
i->insert(3, 2, 8);
i->insert(2, 0, 3);
i->insert(2, 4, 3);
i->insert(3, 2, 8);
i->print();
s->insert(0, 2, "North");
s->insert(4, 2, "South");
s->insert(2, 4, "East");
s->insert(2, 0, "West");
s->print();
return 0;
}
任何想法为什么我会遇到分段错误?