2011-02-05 68 views
0

我正在使用EasyBMP庫。該庫有一個方法:訪問成員函數時遇到問題

int red = image(i, j)->Red; 
// Gets the value stored in the red channel at coordinates (i, j) of the BMP named image and stores it into the int named red. 

下面是我的代碼:

int red = images[i](x, y)->Red; //圖像是一個動態數組,我使用了一個for循環這裏

圖像是一個成員變量這一聲明的類:

Image **images; 

我得到的錯誤是:

scene.cpp:195: error: ‘*(((Image**)((const Scene*)this)->Scene::images) + ((Image**)(((long unsigned int)i) * 8ul)))’ cannot be used as a function 

然而,這工作得很好,但我不知道爲什麼上面不工作:

images[i]->TellWidth() //gets the width of the image 

我明白的地方其混錯,但我不知道如何解決它。有任何想法嗎?

回答

2

要回答你的問題,你有一個指針數組來Image秒。爲數組下標給你一個指針。您必須首先解除指針的引用,然後才能調用其上的函數。

int red = (*(images[i]))(x, y)->Red; 

注意,需要一對額外的圓括號,因爲引用操作*比函數調用操作()較低的優先級。下標運算符[]與函數調用運算符()具有相同的優先級。

// Order: array subscript, function call, arrow 
int red = images[i](x, y)->Red 
// Order: array subscript, function call, pointer dereference, arrow 
int red = *(images[i])(x, y)->Red; 
// Order: array subscript, pointer dereference, function call, arrow 
int red = (*(images[i]))(x, y)->Red; 

如果您對運算符的優先順序有疑問,請使用圓括號!

如果整個數組指針東西還在迷惑你,想想ints數組:

int* arrayOfInts; 

當你下標的arrayOfInts,你會得到一個int

int val = arrayOfInts[0]; 

現在你有指向Images的指針數組。就拿上面的例子和intImage*取代:

Image** arrayOfPointersToImages = GetArrayOfPointersToImages(); 
Image* ptr = arrayOfPointersToImages[0]; 

但是,爲什麼你有一個指針數組來Image就像那樣?你不能用std::vector<Image>

0

你試過

int red = (*(images[i]))(x, y)->Red; 

images是指針表,所以images[i]爲您提供了指針Image,並呼籲operator()你必須使用*獲得images[i]指針的值。

+0

還沒有得到同樣的錯誤,這 – iRobot 2011-02-05 03:01:50

+0

和編輯後?`(*(images [i]))(x,y) - > Red` – 2011-02-05 03:06:39