我试图在我的主程序中调用一个以函数作为参数的类函数,并将该函数应用于私有列表。我收到错误消息invalid conversion from char to char (*f)(char)
。希望我只是不明白如何将函数作为参数传递。以下是我的主 cpp 文件中的函数
char ToUpper(char c)
{
char b='A';
for(char a='a';a<='z';a++)
{
if(a==c)
{
c=b;
break;
}
++b;
}
return c;
}
void upperList(LineEditor line)
{
char c;
for(int i=0;i<100;i++) //ensure iterator is at beginning of line
line.left();
for(int i=0;i<100;i++)
{
c=line.at(); //assign character current element pointed to by iterator
line.apply(ToUpper(c)); //problem: trying to apply ToUpper function to char c
line.right(); //apply function and increment iterator
}
}
这是应用成员函数
void LineEditor::apply(char (*f)(char c))
{
*it=f(c);
}
此外,如果不是很明显,我尝试使用 cctypes toupper 和 tolower,但它们采用并返回整数。