我正在尝试将函数作为参数传递给另一个带有void指针的函数,但它不起作用
#include <iostream>
using namespace std;
void print()
{
cout << "hello!" << endl;
}
void execute(void* f()) //receives the address of print
{
void (*john)(); // declares pointer to function
john = (void*) f; // assigns address of print to pointer, specifying print returns nothing
john(); // execute pointer
}
int main()
{
execute(&print); // function that sends the address of print
return 0;
}
Run Code Online (Sandbox Code Playgroud)
事情是void函数指针,我可以做一个更简单的代码
#include <iostream>
using namespace std;
void print();
void execute(void());
int main()
{
execute(print); …
Run Code Online (Sandbox Code Playgroud) 我有这个功能:
void check_open (ifstream& file)
{
if (not file.is_open())
{
cout << "Error." << endl;
exit(1);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我只能传递ifstream参数,我怎么能让它接受ofstream参数呢?