c++ - <未解析的重载函数类型>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15841338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ - <unresolved overloaded function type>
提问by Mordrag
In my class called Mat
, I want to have a function which takes another function as a parameter. Right now I have the 4 functions below, but I get an error in when calling print(). The second line gives me an error, but I don't understand why, since the first one works. The only difference is function f
is not a member of the class Mat
, but f2
is.
The failure is: error: no matching function for call to Mat::test( < unresolved overloaded function type>, int)'
在我的类中Mat
,我想要一个函数,它将另一个函数作为参数。现在我有下面的 4 个函数,但是在调用 print() 时出现错误。第二行给了我一个错误,但我不明白为什么,因为第一行有效。唯一的区别是 functionf
不是 class 的成员Mat
,而是f2
。失败是:error: no matching function for call to Mat::test( < unresolved overloaded function type>, int)'
template <typename F>
int Mat::test(F f, int v){
return f(v);
}
int Mat::f2(int x){
return x*x;
}
int f(int x){
return x*x;
}
void Mat::print(){
printf("%d\n",test(f ,5)); // works
printf("%d\n",test(f2 ,5)); // does not work
}
Why does this happen?
为什么会发生这种情况?
回答by
The type of pointer-to-member-function
is different from pointer-to-function
.
的类型pointer-to-member-function
不同于pointer-to-function
。
The type of a function is different depending on whether it is an ordinary function or a non-static member functionof some class:
函数的类型取决于它是普通函数还是某个类的非静态成员函数:
int f(int x);
the type is "int (*)(int)" // since it is an ordinary function
And
和
int Mat::f2(int x);
the type is "int (Mat::*)(int)" // since it is a non-static member function of class Mat
Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)"
注意:如果是类 Fred 的静态成员函数,则其类型与普通函数相同: "int (*)(char,float)"
In C++, member functions have an implicit parameter which points to the object (the this pointer inside the member function). Normal C functions can be thought of as having a different calling convention from member functions, so the types of their pointers (pointer-to-member-function vs pointer-to-function) are different and incompatible.C++ introduces a new type of pointer, called a pointer-to-member, which can be invoked only by providing an object.
NOTE: do not attempt to "cast" a pointer-to-member-function into a pointer-to-function; the result is undefined and probably disastrous. E.g., a pointer-to-member-function is not required to contain the machine address of the appropriate function.As was said in the last example, if you have a pointer to a regular C function, use either a top-level (non-member) function, or a static (class) member function.
在 C++ 中,成员函数有一个指向对象的隐式参数(成员函数内部的 this 指针)。可以认为普通 C 函数具有与成员函数不同的调用约定,因此它们的指针类型(指向成员函数的指针与指向函数的指针)不同且不兼容。C++ 引入了一种新的指针类型,称为指向成员的指针,它只能通过提供一个对象来调用。
注意:不要试图将指向成员函数的指针“强制转换”为指向函数的指针;结果是不确定的,可能是灾难性的。例如,指向成员函数的指针不需要包含适当函数的机器地址。如上一个示例中所述,如果您有一个指向常规 C 函数的指针,请使用顶级(非成员)函数或静态(类)成员函数。
回答by Barry
The problem here is that f2
is a method on Mat
, while f
is just a free function. You can't call f2
by itself, it needs an instance of Mat
to call it on. The easiest way around this might be:
这里的问题是这f2
是一个方法 on Mat
,而f
只是一个自由函数。你不能自己调用f2
,它需要一个实例Mat
来调用它。解决此问题的最简单方法可能是:
printf("%d\n", test([=](int v){return this->f2(v);}, 5));
The =
there will capture this
, which is what you need to call f2
.
在=
那里将捕获this
,这是你需要叫什么f2
。
回答by CourageousPotato
I got this error from not using parentheses for a function call. I had this:
我因为函数调用没有使用括号而得到这个错误。我有这个:
Eigen::Quaterniond value;
...
myFunction(value.x);
Eigen::Quaterniond value;
...
myFunction(value.x);
And I fixed this by changing the last line to this:
我通过将最后一行更改为以下内容来解决此问题:
myFunction(value.x());
myFunction(value.x());