gpt4 book ai didi

c++ - 当类及其方法存储在指针数组中时如何调用它

转载 作者:行者123 更新时间:2023-11-28 03:13:31 24 4
gpt4 key购买 nike

如何使用指针并调用它指向的类方法?

例如:

Image *img[26];

Image IM = outputImage();
img[0] = &IM;

我想调用 img[0] 或 IM 的方法。我尝试过类似的操作,但收到错误。

img[0].getPixel(0,1);

错误是“表达式必须有类类型”

最佳答案

由于您使用的是指针数组,因此必须将其解引用为指针。

 img[0]->getPixel(0, 1);

还有这个:

 Image IM = outputImage();

应该是:

 Image &IM = outputImage();

假设 outputImage() 返回一个引用。

关于c++ - 当类及其方法存储在指针数组中时如何调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17652671/

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