2017-10-20 11 views
0

Je faisais un projet pour une classe (déjà soumis), mais la violation d'accès en lecture lorsque j'initialise le tableau dynamique int 2D me dérange encore et je ne sais pas ce qui cause il.violation d'accès en lecture lors de l'initialisation 2d tableau dynamique 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; 
     } 
    } 

L'erreur n'a pas montré avant, mais quand je décidé de courir à nouveau le programme avant de soumettre, la violation d'accès de lecture est apparu, donc je ne suis pas sûr ce qui a causé.

+0

Qu'est-ce que 'aPoint'? – Ron

+3

'int j = 0; i Mat

+0

' pour (int j = 0; i P0W

Répondre

2

Modifier ceci:

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

à ceci:

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

puisque vous voulez vérifier l'état de j, non i.