2013-11-22 114 views
0

當我運行下面的程序,我寫我得到運行時檢查失敗#2堆棧變量'NewImage'周圍已損壞。我怎樣才能解決這個問題?我知道它必須處理變量內存限制,但我不知道要改變什麼。謝謝!C++調試錯誤

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

const int ROWS = 16; 
const int COLS = 16; 

enum flipType {HORIZONTAL, VERTICAL}; 
enum rotateType {CLOCKWISE, COUNTER_CLOCKWISE}; 
void getImage(char img[][COLS]); 
void print(const char img[][COLS], string msg); 
void flip(const char img[][COLS], char NewImage[][COLS], flipType); 
void negative(const char img[][COLS], char NewImage[][COLS]); 
void rotate(const char img[][COLS], char NewImage[][COLS], rotateType); 

int main() 
{ 
    char Image[ROWS][COLS]; 
    char NewImage[ROWS][COLS]; 
    getImage(Image); 
    // print the original image 
    print(Image, "Original Image"); 
    getImage(NewImage); 
    flip(Image, NewImage, VERTICAL); 
    print(NewImage, "Vertically Flipped Image"); 
    getImage(Image); 
    flip(Image, NewImage, HORIZONTAL); 
    print(NewImage, "Horizontally Flipped Image"); 
    getImage(Image); 
    negative(Image, NewImage); 
    print(NewImage, "Negative Image"); 
    getImage(Image); 
    rotate(Image, NewImage, CLOCKWISE); 
    print(NewImage, "Clockwise Rotated Image"); 
    getImage(Image); 
    rotate(Image, NewImage, COUNTER_CLOCKWISE); 
    print(NewImage, "Counter-Clockwise Rotated Image"); 
    return 0; 
} 
void getImage(char img[][COLS]) 
{ 
    char discard; 
    //Load image from file 
    ifstream imgFile; 
    imgFile.open("16X16L.txt"); 
    for (int i=0; i<ROWS; i++) 
    { 
     for (int j=0; j<COLS; j++) 
     { 
      char inputChar; 
      imgFile.get(inputChar); 
      // Convert digital to blank or asterisk 
      if (inputChar == '0') 
       img[i][j] = ' '; 
      else 
       img[i][j] = '*'; 
     } 
     // throw away the newline character 
     imgFile.get(discard); 
    } 
} 
void print(const char img[][COLS], string msg) 
{ 
    cout << msg << endl; 
    for (int i=0; i < ROWS; i++) 
    { 
     for (int j=0; j < COLS; j++) 
     { 
      cout << img[i][j]; 
     } 
     cout << endl; 
    } 
} 

void flip(const char img[][COLS], char NewImage[][COLS], flipType flipDir) 
{ 
    if(flipDir == HORIZONTAL) 
    { 
     for (int i=0; i<ROWS; i++) 
     { 
      for (int j=0; j<COLS; j++) 
      { 
       NewImage[i][-j-1]= img[i][j]; 
      } 
     } 
    } 
    else if (flipDir == VERTICAL) 
    { 
     for (int i=0; i<COLS; i++) 
     { 
      for (int j=0; j<ROWS; j++) 
      { 
       NewImage[ROWS-1-i][j]= img[i][j]; 
      } 
     } 

    } 
} 

void negative(const char img[][COLS], char NewImage[][COLS]) 
{ 
    for (int i=0; i<ROWS; i++) 
    { 
     for (int j=0; j<COLS; j++) 
     { 
      if (img[i][j] == ' ') 
       NewImage[i][j] = '*'; 
      else NewImage[i][j] = ' '; 
     } 
    } 
} 

void rotate(const char img[][COLS], char NewImage[][COLS], rotateType rotateDir) 
{ 
    if (rotateDir == CLOCKWISE) 
    { 
     for(int i=0; i<COLS; i++) 
     { 
      for(int j=0; j<ROWS; j++) 
      { 
       NewImage[i][j] = img[ROWS-1-j][i]; 
      } 
     } 
    } 
    else 
    { 
     for(int i=0; i<COLS; i++) 
     { 
      for(int j=0; j<ROWS; j++) 
      { 
       NewImage[i][j] = img[j][COLS-1-i]; 
      } 
     } 
    } 
} 
+1

漂亮請用草莓和櫻桃頂上,張貼代碼時使用正確的縮進! – Angew

+0

你能否把這個減少到最小樣本,又名[SSCCE](http://sscce.org/)?或者至少告訴我們* NewImage報告錯誤。 – Angew

回答

5

替換此

NewImage[i][-j-1]= img[i][j]; 

與此

NewImage[i][COLS-j-1]= img[i][j]; 

不知道爲什麼你不能當場表示自己因爲你得到了垂直翻轉正確的,你有一個很好的理解問題是什麼。需要學習仔細查看你自己的代碼,我猜。