2015-10-15 48 views
-5

下面的代碼是給我下面的錯誤需要與分割錯誤

錯誤說明援助:0000005:訪問衝突讀取位置0xCDCDCDD5在0x00DC5D81在ImageComponent2.exe 未處理的異常。

// ImageComponents

#include <iostream> 
#include "Position.h" 

using namespace std; 

void labelComponents(int size, int **pixel); 
void outputImage(int size, int **pixel); 

int main(){ 

int size = 0; 

cout << "Enter image size: "; 
cin >> size; 

int ** pixel = new int *[size + 2]; 
for (int i = 1; i <= size; i++) 
{ 
    pixel[i] = new int[size + 2]; 
} 

cout << "Enter the pixel array in row-major order:\n"; 
for (int i = 1; i <= size; i++) 
    for (int j = 1; j <= size; j++) 
    { 
     cin >> pixel[i][j]; 
    } 

labelComponents(size, pixel); 
outputImage(size, pixel); 


system("pause"); 
return (0); 
} 


void labelComponents(int size, int **pixel){ 
// initialize offsets 
Position * offset = new Position[4]; 
offset[0] = Position(0, 1); // right 
offset[1] = Position(1, 0); // down 
offset[2] = Position(0, -1); // left 
offset[3] = Position(-1, 0); // up 

int numNbr = 4; // neighbors of a pixel position 
Position * nbr = new Position(0, 0); 
Position * Q = new Position[size * size]; 
int id = 1; // component id 
int x = 0; // (Position Q) 

// scan all pixels labeling components 
for (int r = 1; r <= size; r++)  // row r of image 
    for (int c = 1; c <= size; c++) // column c of image 
    { 
     if (pixel[r][c] == 1) 
     {// new component 
      pixel[r][c] = ++id; // get next id 
      Position * here = new Position(r, c); 

      do 
      {// find rest of component 
       for (int i = 0; i < numNbr; i++) 
       {// check all neighbors of here 
        nbr->setRow(here->getRow() + offset[i].getRow()); 

        nbr->setCol(here->getCol() + offset[i].getCol()); 

        if (pixel[nbr->getRow()][nbr->getCol()] == 1) 
        {// pixel is part of current component 
         pixel[nbr->getRow()][nbr->getCol()] = id; 
         Q[x] = *nbr; 
         x++; 
        } 
       } 
       // any unexplored pixels in component? 
       *here = Q[x]; // a component pixel 
       x--; 
      } while (here != NULL); 
     } // end of if, for c, and for r 
    } 
} // end of labelComponents 

void outputImage(int size, int **pixel){ 
cout << "The labeled image is: "; 
for (int i = 1; i <= size; i++){ 
    cout << endl; 
    for (int j = 1; j <= size; j++) 
     cout << pixel[i][j] << " "; 
} 
} // end of outputImage 

//Position.h

#ifndef POSITION_H 
#define POSITION_H 

class Position 
{ 
private: 

int row; // row number of the position 
int col; 
// column number of the position 

public: 
Position(); // default 
Position(int theRow, int theCol); // parameter 
Position(const Position & aPosition); // copy 
Position & operator = (const Position & aPosition); // overload = 

// overload = 

// mutators 
void setRow (int r); 
void setCol (int c); 
//accessors 
int getRow() const; 
int getCol() const; 
}; // end Position 

Position::Position() 
{ 
setRow(0); 
setCol(0); 
} 

Position::Position(int r, int c) 
{ 
    setRow(r); 
    setCol(c); 
} 

Position::Position(const Position & aPosition) 
{ 
setRow(aPosition.row); 
setCol(aPosition.col); 
} 

Position & Position::operator=(const Position & aPosition) 
{ 
    this->row=aPosition.row; 
    this->col=aPosition.col; 
    return(*this); 
    } 

void Position::setRow(int r) 
{ 
this->row = r; 
} 
void Position::setCol(int c) 
{ 
this->col = c; 
} 
int Position::getRow() const 
{ 
return this->row; 
} 
int Position::getCol() const 
{ 
return this->col; 
} 

#endif 
+0

你嘗試調試應用程序?線後調試線,直到找到導致錯誤的線 - 想想這個地方。 –

回答

2

在C/C++,arraw索引去從0n-1,而不是從1n。你所有的for循環是錯誤的:

for (int i = 1; i <= size; i++) 

必須替換爲:

for (int i = 0; i < size; i++) 

否則,你size位置是越界訪問數組。

使用調試器和/或較小的一段代碼工作將使它更容易爲你算出這個;-)