iRobot
iRobot

Reputation: 79

Trouble with accessing a member function

I'm using the EasyBMP library. This library has a method:

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.

Here's the code that I have:

int red = images[i](x, y)->Red;//images is a dynamic array, I'm using a for loop here

images is a member variable of the class with this declaration:

Image **images;

The error I'm getting is:

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

However, this works fine, but I dont know why the above doesnt work:

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

I understand where its getting mixed up, but I dont know how to fix it. Any ideas?

Upvotes: 0

Views: 110

Answers (2)

Pawel Zubrycki
Pawel Zubrycki

Reputation: 2713

Have you tried

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

images is a table of pointers, so images[i] gives you pointer to Image and to call operator() you have to use * to get the value of images[i] pointer.

Upvotes: 0

In silico
In silico

Reputation: 52129

To answer your question, you have an array of pointers to Images. Subscripting the array gives you a pointer. You have to dereference the pointer first before you can call functions on it.

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

Note that an extra pair of parentheses are needed because the dereference operator * has a lower precedence than the function call operator (). The subscripting operator [] has the same precedence as the function call operator ().

// 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;

If you're in doubt about the precedence order of the operators, use parentheses!

If the whole array to pointer thing still confuses you, think about an array of ints:

int* arrayOfInts;

When you subscript an arrayOfInts, you get an int:

int val = arrayOfInts[0];

Now you have an array of pointers to Imagess. Take the above example and replace int with Image*:

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

But why do you have an array of pointers to Images like that? Can't you use a std::vector<Image>?

Upvotes: 2

Related Questions