c++:没有匹配的调用函数,但候选者具有完全相同的签名
时, g++ 会抱怨。
myclass.cxx:185: error: no matching function for call to 'IMyInterface::doSomething(const SomeClass*, unsigned int)'
IMyInterface.h:34: note: candidates are: virtual void IMyInterface::doSomething(const SomeClass*&, unsigned int)
当我调用
m_instanceOfInterface->doSomething((const SomeClass*)0,(unsigned int)1);
Anypoints to Why? 在我看来,g++ 在声明的内容和正在调用的内容之间看到完全相同的签名,但仍然抱怨找不到匹配的函数。
我可以在同一上下文中调用 IMyInteface
的另一个函数 IMyInterface::doSomethingElse(float& p)
。那么不知何故 const
是问题所在?
我没有传递 NULL 指针并强制转换一个常量整数只是为了好玩......最初我有
m_instanceOfInterface->doSomething((const SomeClass*)m_someDerivedClass,m_anInteger);
并得到了相同的错误。所以我决定通过给出一些明确的参数来澄清 g++ 的问题。我可以向你保证 NULL 指针不是问题 - 尽管可以理解的是,当看到 NULL 被用 const 传递时,我们都有点畏缩:)
g++ complains about
myclass.cxx:185: error: no matching function for call to 'IMyInterface::doSomething(const SomeClass*, unsigned int)'
IMyInterface.h:34: note: candidates are: virtual void IMyInterface::doSomething(const SomeClass*&, unsigned int)
when I call
m_instanceOfInterface->doSomething((const SomeClass*)0,(unsigned int)1);
Any pointers to why? It seems to me that g++ is seeing exactly the same signature between what is declared and what is being called, but still complains about no matching function found.
I can call, in the same context, another function of IMyInteface
, IMyInterface::doSomethingElse(float& p)
. So somehow the const
is the problem?
I did not pass the NULL pointer and cast a constant integer just for fun... originally I have
m_instanceOfInterface->doSomething((const SomeClass*)m_someDerivedClass,m_anInteger);
and got the same error. So I decided to clarify things with g++ by giving some explicit arguments. I can assure you that the NULL pointer is NOT the problem - although understandably we all cringed a bit when seeing a NULL being passed with const
:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该函数需要通过非常量引用作为参数,该引用不能绑定到临时变量。观察:
在您的情况下,
T = SomeClass const *
。因此,您提供了一个非临时的:请注意,这样做的目的大概是用一些有意义的值填充
pc
,因此请务必适当地合并它。The function requires its argument by non-const reference, which cannot bind to a temporary. Observe:
In your case,
T = SomeClass const *
. So, you have provide a non-temporary:Note that the purpose of this is presumably to fill
pc
with some meaningful value, so be sure to incorporate that apporpriately.编辑
已更正以反映评论。
候选人不有完全相同的签名。该函数获取对
SomeClass
的 const 指针的引用,并且您正在提供一个指针。事实上,您正在提供一个特别令人讨厌的函数指针。这里的一个大问题是空指针。您需要为编译器提供一些可靠的东西,以便它可以引用它。您给它一个空指针。
Edit
Corrected to reflect comments.
The candidate does not have the exact same signature. The function takes a reference to a const pointer to a
SomeClass
, and you are providing a pointer. In fact, you are providing a particularly nasty pointer to the function.One big problem here is that null pointer. You need to give the compiler something solid so it can take the reference of it. You are giving it a null pointer.
您的函数需要一个对指针的非常量引用,而不是一个指针。您无法从文字(即空指针)创建非常量引用,因此编译器无法调用第一个参数为“0”的函数,并且他会查找采用其第一个参数(指针)的重载)通过复制。
Your function require a non-const reference to a pointer, not a pointer. You can't create a non-const reference from a literal (i.e. the null pointer), so the compiler is unable to call the function with the first argument being '0' and he looks for an overload taking its first argument (the pointer) by copy.