2012-10-25 42 views
1

我正面臨二維數組指針的問題。它編譯沒有錯誤,但是當我嘗試運行該文件時,我得到的只是一行說我有分段錯誤。C++二維數組指針分段錯誤

我的頭文件:

#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; 
} 

任何想法,爲什麼我得到一個分段錯誤?

+0

你會得到具體的行嗎?你在什麼環境?你嘗試過調試嗎? – Danpe

回答

5

這是一個錯誤:

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; 
    } 
} 

i在遞增兩個外和內for和最終將導致i等於numRows和去一個過去的陣列的端部,這是未定義的行爲以及分段故障的可能原因。

由於TwoDArray中有一個動態分配的成員,因此您需要防止複製TwoDArray的實例或實現賦值運算符和複製構造函數(請參閱What is The Rule of Three?)。

如果這不是學習練習,您可以使用vector<vector<T>>來代替。

此外,作爲陣列的尺寸是編譯時間常數有可能使他們的模板參數也完全避免動態內存:

template <typename TType, int TRows, int TCols> 
class TwoDArray { 
    private: 
    TType theArray[TRows][TCols]; 
    TType defSpace; 
    .... 

TwoDArray<int, 5, 5> i(0); 

看到http://ideone.com/dEfZn5全演示。

+0

@JohnnyMopp看起來像學習使用調試器的好時機:) – maniek

+0

這幫助了我的一部分,但現在它的字符串數組存在段錯誤,但謝謝! – RazGarth

+0

@RazGarth,我看不出是什麼問題,看起來很好:http://ideone.com/EdrY7J – hmjd