2014-09-05 74 views
0

假設rgbapixel是rgbapixel.h文件中的一類像素,所以它具有綠色,藍色,紅色等公共成員的顏色。 PNG是png.h文件中的一類圖像,因此它具有圖像寬度和高度作爲私有成員,然後它有兩個公共函數返回寬度和高度。我應該如何糾正這個指針問題,它給我一個錯誤

在我的main.cpp中,這裏是代碼;

// sets up the output image 
PNG * setupOutput(int w, int h) 
{ 
    PNG * image = new PNG(w, h); 
    return image; 
} 
void sketchify() 
{ 
    // Load in.png 
     PNG * original = new PNG; 
    original->readFromFile("in.png"); 
    int width = original->width(); 
    int height = original->height(); 

    // Create out.png 
    // PNG * output;     // i change this 
    PNG * output = new PNG; 
    setupOutput(width, height); 

    // Loud our favorite color to color the outline 
    RGBAPixel * myPixel = myFavoriteColor(192); 

    // Go over the whole image, and if a pixel differs from that to its upper 
    // left, color it my favorite color in the output 
    for (int y = 1; y < height; y++) 
    { 
      for (int x = 1; x < width; x++) 
      { 
        // Calculate the pixel difference 
        RGBAPixel * prev = (*original)(x-1, y-1);  // previous top lfet 
        RGBAPixel * curr = (*original)(x , y );  // current 

             // subtracting to see diffrence between pixels 
        int diff = abs(curr->red - prev->red ) + 
             abs(curr->green - prev->green) + 
             abs(curr->blue - prev->blue); 

        // If the pixel is an edge pixel, 
        // color the output pixel with my favorite color 

        RGBAPixel * currOutPixel = (*output)(x,y); 
        if (diff > 100) 
          currOutPixel = myPixel;   // something wrong 
      } 
    } 
    // Save the output file 
    output->writeToFile("out.png"); 
    // Clean up memory 
    delete myPixel; 
    delete output; 
    delete original; 

當我執行代碼時,出現類似錯誤;

[EasyPNG]: Warning: attempted to access non-existent pixel (407, 306); 
     Truncating request to fit in the range [0,0] x [0,0]. 

[EasyPNG]: Warning: attempted to access non-existent pixel (408, 306); 
     Truncating request to fit in the range [0,0] x [0,0]. 

我在哪裏寫的「什麼是錯的」,我已被告知,這裏面就有一個錯誤。我沒看到它。 'mypixel'和currout,兩者都成功地作爲指針聲明,所以我不明白這個聲明怎麼會是錯誤的。如果我試圖改變它,我會遇到編譯錯誤。幫助

+1

我沒有看到任何設置輸出大小的地方。 – user3344003 2014-09-05 01:10:34

+0

我沒有看到任何輸出「EasyPNG」的地方,這意味着這是_not_你的[testcase](http://sscce.org)。 – 2014-09-05 02:19:55

回答

1

setupOutput(width, height);沒有做任何有用的事情。它會在堆上創建一個新的PNG,但返回的值將被丟棄。您以兩個問題結束:

  1. output沒有正確設置其寬度和高度。
  2. 存在內存泄漏。

您可以使用以下兩種方法之一來解決該問題。

  1. setupOutput(width, height)的返回值分配給output

    更換線路:

    PNG * output = new PNG; 
    setupOutput(width, height); 
    

    PNG * output = setupOutput(width, height); 
    
  2. 不要使用setupOutput(width, height);在所有。內聯創建一個PNG

    更換線路:

    PNG * output = new PNG; 
    setupOutput(width, height); 
    

    PNG * output = new PNG(width, height); 
    

不能保證這些變化將解決所有的問題。

更新,響應由OP

     currOutPixel = myPixel;   // something wrong 

評論沒有做任何有用的東西要麼。它僅覆蓋本地變量currOutPixel指向的地方。假設可以分配RGBAPixel類型的對象,則需要:

     *currOutPixel = *myPixel; 
+0

因此「currOutPixel = myPixel;」沒有任何問題......「電訊管理局告訴我,在那裏有東西 – user124627 2014-09-05 02:40:12

+0

@ user124627,看我的更新。 – 2014-09-05 02:45:14

+0

非常感謝,該程序後工作 – user124627 2014-09-05 23:32:13