没有匹配的函数调用,使用函数指针
No matching function for call, using a function pointer
#include <TestAssert.h>
...
if (TestAssert::Equals(10, "x", 10, "y", 10, f_TestFailedMsg, v_TCResult,
s_TestCaseCtrl)) { return; }
f_TestFailedMsg(在基类中)的声明:
void A_AbstractTestStub_Actor::f_TestFailedMsg( const char *
apc_FormatString, ... )
{
TestAssert.h (Test_failed_callback_function和Equals的定义)部分:
typedef void (*Test_failed_callback_function)(T_String);
static bool Equals( int lineNumber, T_String valueText, int value, T_String
expectedText, int expected, Test_failed_callback_function testFailedMsg,
bool & tcResult, P_testCaseCtrl::Base & testCaseCtrl );
Equals的实现(TestAssert.cpp):
bool TestAssert::Equals( int lineNumber, T_String valueText, int value,
T_String expectedText, int expected, Test_failed_callback_function
testFailedMsg, bool & tcResult, P_testCaseCtrl::Base & testCaseCtrl )
{
...
我得到错误:
no matching function for call to 'TestAssert::Equals(int const char [29],
int&, const char[4], int, <unresolved overloaded function type>, bool&,
P_testCaseCtrl::Base&)
Tester.cpp:181:89: note: candidate is:
../TestAssert.h:30:17: note: static bool TestAssert::Equals(int, T_String,
int, T_String, int, Test_failed_callback_function, bool&,
P_testCaseCtrl::Base&)
../TestAssert.h:30:17: note: no known conversion for argument 6
from '<unresolved overloaded function type>'
to 'Test_failed_callback_function {aka void (*)(std::basic_string<char>)}'
如何解决这个错误?
f_TestFailedMsg
为非静态成员函数;它需要调用A_AbstractTestStub_Actor
类型的对象。不能绑定void (*)(T_String)
有几种方法可以解决这个问题。如果f_TestFailedMsg
不需要对象,则将其设置为静态。
如果你想提供成员函数作为参数,并获得一个对象稍后调用它,你可以改变你的Test_failed_callback_function
为成员函数指针:
typedef void (A_AbstractTestStub_Actor::*Test_failed_callback_function)(T_String)
显然,这将限制函数是A_AbstractTestStub_Actor
的成员,但是你可以做一些模板魔术来接受其他类型的成员。
如果c++ 11可用,最具表现力的选择是将Test_failed_callback_function
变为std::function<void(T_String)>
,然后用lambda或std::bind
绑定对象:
if (TestAssert::Equals(10, "x", 10, "y", 10,
[&my_obj](T_String s){my_obj.f_TestFailedMsg(s);},
v_TCResult, s_TestCaseCtrl))
{ return; }
相关文章:
- QMetaObject invokeMethod的基于函数指针的语法
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- c++r值引用应用于函数指针
- 模板函数指针和lambda
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 带有类的函数指针
- () 函子后面的括号,而不是函数指针?
- 全局作用域中函数指针的赋值
- 使用"Task"函数指针队列定义作业管理器
- 将成员函数指针作为参数传递给模板方法
- 如何创建对象函数指针C++映射?
- 匹配函数指针作为模板参数?
- 通过函数指针定义类范围之外的方法
- 存储在类中的函数指针
- C++从函数指针数组调用函数
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 整数键映射到头文件中的成员函数指针
- 从类成员函数到类 C 函数指针的转换
- 如何将内联匿名函数分配给C++函数指针
- 将字符缓冲区强制转换为函数指针