2014-08-31 29 views
1

假設圖像是我的對象文件「a」,我創建了另一個名爲b的對象文件來旋轉圖像。我給了(j,i)一個給(i,j)的b,但是我的代碼不工作,因爲我沒有正確地使用那個稱爲operator的函數,我有「//」我嘗試過的東西,但是我不斷出現錯誤,我該怎麼辦? rgbapixels是一個像素類,就像png是一個圖像類;如何旋轉給定的圖像?

在PNG.h中,我們現在將函數定義爲;

#include <iostream> 
#include "rgbapixel.h" 
class PNG 
{ 
public: 
RGBAPixel * operator()(size_t x, size_t y); 
/** 
* Const pixel access operator. Const version of the previous 
* operator(). Does not allow the image to be changed via the 
* pointer. 
* @param x X-coordinate for the pixel pointer to be grabbed from. 
* @param y Y-cooridnate for the pixel pointer to be grabbed from. 
* @return A pointer to the pixel at the given coordinates (can't 
*  change the pixel through this pointer). 
*/ 
size_t width() const; // returns width 
size_t width() const; 
private: 
// storage 
size_t _width; 
size_t _height; 
RGBAPixel * _pixels; 
}; 

函數在png.cpp中爲我們實現。所以,在main.cpp中,我有我的代碼來使用它們;

#include <algorithm> 
#include <iostream> 

#include "rgbapixel.h" 
#include "png.h" 
using namespace std; 
int main() 
{ 
cout << "me..........." << endl; 
PNG a("I have put the image in "a" here"); 
PNG b; 
for(size_t i = 0; i < a.width(); i++) 
{ 
for(size_t j = 0; j <a.height(); j++) 
{ 
// *b(i, j) = *a(j, i);        erata 
// b(i,j) = RGBAPixel * operator()(size_t x, size_t y); 
// b(i, j) = operator()(i, j);    
//b(i,j) = *operator(i,j); 
//b(j,i) = a*operator(i,j); 
//b(j,i) = a.operator(i,j); 
//b(j, i) = a.*operator(i,j); 
} 
} 
return 0; 
} 

我得到錯誤使用該功能,我不能說出了什麼問題。所以,我不知道我的alogarithm是否會得到旋轉的圖像

有些錯誤;

[[email protected] lab_intro]$ make 
clang++ -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -pedantic main.cpp 
main.cpp:24:29: error: expected ')' 
b(i,j) = *operator(i,j); 
        ^
main.cpp:24:28: note: to match this '(' 
b(i,j) = *operator(i,j); 
       ^
main.cpp:24:20: error: use of undeclared 'operator()' 
b(i,j) = *operator(i,j); 
     ^
2 errors generated. 
make: *** [main.o] Error 1 

感謝

回答

0

()運營商獲取在與該運營商的類的實例被調用使用作爲函數調用。

所以,隨着您的PNG類,和它的兩個實例名爲ab,他們()運營商將得到根本援引爲:

a(i, j) 

b(i, j) 

由於您()運營商的回報一個RGBAPixel *,這樣一個僞函數調用的最終結果是一個指針,我認爲應該解除引用。

在您的示例代碼的第一註釋掉行:

// *b(i, j) = *a(j, i); 

這在我看來就像你()操作的正確用法。我沒有看到編譯該問題的任何問題。

+0

但它失敗了,它給了我錯誤; clang ++ -std = C++ 1y -stdlib = libC++ -c -g -O0 -Wall -Wextra -pedantic main.cpp main.cpp:21:62:error:使用未聲明的標識符'erata' * b(i ,j)= * a(j,i); erata ^ 生成1個錯誤。 make:*** [main.o]錯誤1 – user124627 2014-09-01 01:10:19

+0

無論如何,您的錯誤消息明確提及'erata'。該錯誤消息似乎與賦值語句沒有任何關係。有用的提示:在放棄並要求其他人尋求幫助之前,請至少閱讀三次錯誤消息。尤其是鏗鏘的錯誤信息,因其清晰度和實用性而聞名。 – 2014-09-01 01:12:49

+0

它的工作原理,但出於某種原因,當我輸出圖像在「B」,它的空,它表明沒有像素被複制。給出警告; ....... [EasyPNG]:警告:嘗試訪問不存在的像素(511,383); 截斷請求以適合範圍[0,0] x [0,0]。 [jonathan2 @ linux-a1 lab_intro] $ – user124627 2014-09-01 02:18:35

0

你不應該調用以這種方式操作的功能,而不是這樣做:

*b(i, j) = *a(j, i); 
+0

但它失敗了,它給了我錯誤; clang ++ -std = C++ 1y -stdlib = libC++ -c -g -O0 -Wall -Wextra -pedantic main.cpp main.cpp:21:62:error:使用未聲明的標識符'erata'* b(i,j )= * a(j,i);生成的erata^1錯誤。使:*** [main.o]錯誤1 – user124627 2014-09-01 01:12:32

+1

謝謝你們,我apreciate它 – user124627 2014-09-01 07:20:14