gpt4 book ai didi

c++ - 访问成员函数时遇到问题

转载 作者:行者123 更新时间:2023-11-28 03:52:39 25 4
gpt4 key购买 nike

我正在使用 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

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

最佳答案

要回答您的问题,您有一个指向 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>吗? ?

关于c++ - 访问成员函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4904813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com