我正试图在我的主程序中调用一个类函数,它将一个函数作为其参数,并将该函数应用于一个私有列表。我得到了错误信息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函数,但它们接受并返回整数。