C++ C++;二维指针数组分割错误
我面临一个2D指针数组的问题。它编译时没有错误,但是当我试图运行该文件时,我得到的只是一行代码,表明我有一个分段错误 我的头文件:C++ C++;二维指针数组分割错误,c++,pointers,segmentation-fault,multidimensional-array,C++,Pointers,Segmentation Fault,Multidimensional Array,我面临一个2D指针数组的问题。它编译时没有错误,但是当我试图运行该文件时,我得到的只是一行代码,表明我有一个分段错误 我的头文件: #ifndef __TWODARRAY_H__ #define __TWODARRAY_H__ template <typename T> class TwoDArray { private: T** theArray; int numRows; int numCols; T defSpace; public:
#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
\ifndef\uu two darray\H__
#定义两个Darray__
模板
二类达雷{
私人:
T**阵列;
int numRows;
int numCols;
灵巧的空间;
公众:
TwoDArray(int r,int c,T def);
~TwoDArray();
无效插入(int r、int c、T值);
T访问(int r,int c);
消除空洞(内部r、内部c);
作废打印();
int getNumRows();
int getNumCols();
};
#恩迪夫
我的方法:
#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>;
#包括“TwoDArray.h”
#包括
#包括
#包括
//初始化二维数组
模板
TwoDArray::TwoDArray(int r,int c,T def){
断言(r>0&&c>0);
numRows=r;
numCols=c;
defSpace=def;
阵列=新的T*[r];
对于(inti=0;iinsert(4,2,“南”);
s->插入(2,4,“东”);
s->插入(2,0,“西”);
s->print();
返回0;
}
知道我为什么会出现分段错误吗?这是一个错误:
template<typename T>
void TwoDArray<T>::print() {
for(int i=0; i<numRows; i++) {
for(int j=0;j<numCols; i++) { // should be j++, not i++
std::cout << theArray[i][j];
std::cout << " ";
}
std::cout << std::endl;
}
}
请参阅完整演示。这是一个错误:
template<typename T>
void TwoDArray<T>::print() {
for(int i=0; i<numRows; i++) {
for(int j=0;j<numCols; i++) { // should be j++, not i++
std::cout << theArray[i][j];
std::cout << " ";
}
std::cout << std::endl;
}
}
请参阅完整演示。@Johnnymop似乎是学习使用调试器的好时机:)这在某种程度上帮助了我,但现在它的字符串数组存在分段错误,但谢谢@RazGarth,我看不出问题出在哪里,这似乎很好:@Johnnymop似乎是学习使用调试器的好时机:)这在某种程度上帮助了我,但现在它的字符串数组存在分段错误,但谢谢@RazGarth,我看不出问题出在哪里,看起来很好:你有具体的线路吗?你所处的环境是什么?你试过调试吗?你得到具体的线路了吗?你所处的环境是什么?你试过调试吗?
template <typename TType, int TRows, int TCols>
class TwoDArray {
private:
TType theArray[TRows][TCols];
TType defSpace;
....
TwoDArray<int, 5, 5> i(0);