2017-10-20 133 views
0

我正在爲一個類(已提交)做一個項目,但是當初始化int 2D動態數組時,讀取訪問衝突仍然困擾着我,我不知道是什麼原因造成的它。初始化2d動態數組時讀取訪問衝突C++

class kMeans 
{ 
public: 
    //xyCoord struct 
    struct xyCoord 
    { 
     int Label; 
     int xCoordinate; 
     int yCoordinate; 
    }; 

    //variables 
    int K; 
    xyCoord *Kcentroids; 
    int numPts; 
    aPoint *pointSet; 
    int numRow; 
    int numCol; 
    int **imageArray = NULL; 
    int changeLabel; 

    //constructor 
    kMeans(int clusterNum, int numPoints, int row, int col) 
    { 
     //initializes the row and column values 
     numCol = col; 
     numRow = row; 

     //Allocate the row and column as the size of the 2D array 
     imageArray = new int*[row]; 
     for (int i = 0; i < row; i++) 
     { 
      imageArray[i] = new int[col]; 
     } 

     //initializes the 2D array to contain all 0s 
     for (int i = 0; i < row - 1; i++) 
     { 
      for (int j = 0; i < col - 1; j++) 
      { 
       imageArray[i][j] = 0; //read access violation occurs here 
      } 
     } 

     //Allocate numPoints as the size of the array 
     pointSet = new aPoint[numPoints]; 
     numPts = numPoints; 

     //Allocate clusterNum as the size of the array 
     Kcentroids = new xyCoord[clusterNum]; 
     K = clusterNum; 

     //Initialize the labels for each Kcenteroid 
     for (int i = 0; i < K; i++) 
     { 
      Kcentroids[i].Label = i + 1; 
     } 
    } 

錯誤之前沒有露面,但是當我決定提交之前再次運行程序,讀取訪問衝突出現了,所以我不知道什麼原因造成的。

+0

什麼是aPoint? – Ron

+3

'int j = 0;我 Mat

+0

for(int j = 0; i P0W

回答

2

更改此:

for (int j = 0; i < col - 1; j++) 

這樣:

for (int j = 0; j < col - 1; j++) 

,因爲要檢查的條件j,不i