[英]Trouble with accessing a member function
我正在使用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循環
images是具有此聲明的類的成員變量:
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
我知道它在哪里混雜,但我不知道如何解決。 有任何想法嗎?
要回答您的問題,您有一個指向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
s的指針數組。 以上面的例子為例,用Image*
替換int
:
Image** arrayOfPointersToImages = GetArrayOfPointersToImages();
Image* ptr = arrayOfPointersToImages[0];
但是,為什么會有這樣一個指向Image
的指針數組? 你不能使用std::vector<Image>
嗎?
你有沒有嘗試過
int red = (*(images[i]))(x, y)->Red;
images
是一個指針表,因此images[i]
為您提供了指向Image
指針,並調用operator()
時必須使用*
來獲取images[i]
指針的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.