在功能列表中未能调用函数
Failed to call function in a list of functions
我有以下c 代码,该c 代码将2存储在列表中,然后在呼叫中,然后在内部列表中:
#include <string>
#include <queue>
#include <list>
#include <map>
#include <vector>
#include <utility>
#include <iostream>
#include <thread>
#include <Windows.h>
#include <stdio.h>
using namespace std;
typedef void (*fp)();
void simpletest1(){
cout << "test1";
}
void simpletest2(){
cout << "test2";
}
int main()
{
list<fp> generalHandlers;
generalHandlers.push_back(simpletest1);
generalHandlers.push_back(simpletest2);
for (auto it = generalHandlers.begin(); it != generalHandlers.end(); ++it){
(*it)();
}
return 0;
}
但是,在编译和运行后,它没有打印出任何东西,只是在1秒内终止。怎么了?
代码是完全有效的,所以问题在其他地方;
也许您应该尝试在程序退出之前暂停该程序,以便实际上可以阅读输出中的内容。还可以用std::endl
冲洗cout
缓冲区可能会有所帮助。
它对我有用,尽管我用'std :: vector'替换了'列表'。也许您只是没有注意到输出,因为您在cout语句之后没有添加" endl"?
您需要冲洗输出缓冲区。尝试:
void simpletest1(){
cout << "test1" << endl;
}
void simpletest2(){
cout << "test2" << endl;
}
或者,将您的功能独自离开,从main
齐平:
int main()
{
list<fp> generalHandlers;
generalHandlers.push_back(simpletest1);
generalHandlers.push_back(simpletest2);
for (auto it = generalHandlers.begin(); it != generalHandlers.end(); ++it){
(*it)();
}
cout << endl;
return 0;
}
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?