访问成员函数时遇到问题

时间:2011-02-05 02:51:19

标签: c++

我正在使用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; // images是动态数组,我在这里使用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

我明白它在哪里混淆了,但我不知道如何解决它。有什么想法吗?

2 个答案:

答案 0 :(得分: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的指针数组。以上面的示例为例,将int替换为Image*

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

但为什么你有一个指向Image的指针数组呢?你不能使用std::vector<Image>吗?

答案 1 :(得分:0)

你试过吗

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

images是一个指针表,因此images[i]为您提供指向Image的指针,并调用operator()您必须使用*来获取值images[i]指针。

相关问题