下标运算符重载和引用指针错误
我现在正在与一个奇怪的错误作斗争一段时间,我需要你的帮助: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有“不匹配'...'”错误意味着调用中的参数与任何函数或运算符的声明参数都不兼容,因此您应该查看参数声明。将
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.首先,没有必要通过引用传递这么多东西。按值获取 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 actuallyconst
, but you failed to mention that in your paste. Subscript operators generally come in pairs; you need to declare another one asColor<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 specialisedImage
fordouble
.EDIT: By the way, returning
Color<P>*&
has a severe disadvantage; someone could doimg[0] = 0
, which was probably not the way the class was meant to be used.