下标运算符重载和引用指针错误

发布于 2024-12-13 11:49:10 字数 757 浏览 2 评论 0原文

我现在正在与一个奇怪的错误作斗争一段时间,我需要你的帮助:D

我有一个像这样声明的“Image”类,

template <typename P>
class Image {
// ...
public: // Operators 
    Color<P>*& operator [] ( unsigned int const& i );
// ...
}

因为这是我第一次重载这个运算符,我不太确定它(尤其是 * & 部分...)

当我尝试类似的方法时,问题就出现了:

/// Sample Code
Image<P> img; img[0][0] = Color<double>(1.0, 1.0, 1.0);

显然

./src/Graphics/PNGWriter.cc:12:3: error: no match for ‘operator[]’ in ‘img[0]’
./src/Graphics/Image.cc:24:12: note: candidate is: Color<P>*& Image<P>::operator[](const unsigned int&) [with P = double] <near match>

,我的解决方案更加晦涩难懂...

有什么想法吗?

谢谢 !

I'm struggling with a strange error for a while now and I need your help :D

I have an 'Image' class declared like

template <typename P>
class Image {
// ...
public: // Operators 
    Color<P>*& operator [] ( unsigned int const& i );
// ...
}

Since it's the first time I overload this operator, I'm not really sure of it (especially the *& part...)

The troube appears when I tried something like :

/// Sample Code
Image<P> img; img[0][0] = Color<double>(1.0, 1.0, 1.0);

I get

./src/Graphics/PNGWriter.cc:12:3: error: no match for ‘operator[]’ in ‘img[0]’
./src/Graphics/Image.cc:24:12: note: candidate is: Color<P>*& Image<P>::operator[](const unsigned int&) [with P = double] <near match>

Obviously the obscucate a bit more the solution to me...

Any idea ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对岸观火 2024-12-20 11:49:10

所有“不匹配'...'”错误意味着调用中的参数与任何函数或运算符的声明参数都不兼容,因此您应该查看参数声明。将 operator[] 的参数设置为无符号整数。同样,通过引用传递返回的指针也很奇怪;不一定是坏事,但非常可疑。一般来说,非复合类型应该按值传递。

All "no match for '...'" errors mean the arguments in the call aren't compatible to the declared arguments for any functions or operators, so you should look to the argument declarations. Make argument to operator[] an unsigned int. Similarly, passing the returned pointer by reference is quite odd; not necessarily bad, but highly suspect. In general, non-compound types should be passed by value.

吃→可爱长大的 2024-12-20 11:49:10

首先,没有必要通过引用传递这么多东西。按值获取 unsigned int 几乎肯定不会减慢速度;对于指针的引用也是如此。

这可能是问题所在,但我对此表示怀疑;我不明白为什么 0 不会转换为 unsigned int const&。另一种可能性是该图像实际上是 const,但您在粘贴中没有提及这一点。下标运算符通常成对出现;您需要将另一个声明为 Color

; const* 运算符 [] ( unsigned int i ) const;

请注意, img[0] 是被抱怨的问题,因此 = Color(1.0, 1.0, 1.0); 可能不是问题(除非有我缺少一些 SFINAE 错误)。最后,确保您没有将 Image 专门用于 double

编辑:顺便说一句,返回 Color

*& 有一个严重的缺点;有人可以这样做 img[0] = 0,这可能不是该类的使用方式。

First of all, there is no need to pass so many things by reference. Taking the unsigned int by value would almost certainly not slow things down; same about the reference to pointer.

This could be the problem, but I doubt it; I don't see why 0 wouldn't convert to a unsigned int const&. Another possibility is that the image is actually const, but you failed to mention that in your paste. Subscript operators generally come in pairs; you need to declare another one as Color<P> const* operator [] ( unsigned int i ) const;.

Note that img[0] is what is being complained about, so the = Color<double>(1.0, 1.0, 1.0); is probably not an issue (unless there's some SFINAE error I'm missing). Finally, make sure you haven't specialised Image for double.

EDIT: By the way, returning Color<P>*& has a severe disadvantage; someone could do img[0] = 0, which was probably not the way the class was meant to be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文