У меня проблемы с обработкой C ++ карт функций без ошибок.Я делаю простой Round Robin Scheduler, и есть некоторые переменные, например, сколько раз запускалась каждая программа и т. Д., Которые я хочу менять при каждом запуске, поэтому я создал деку указателей на функции, которые я буду выполнять, и когдафункция выскочила, я хочу получить ее «состояние» и изменить ее на карту, но это сложно.Мой код:
#include <map>
#include <iostream>
#include <ctime>
#include <sys/time.h>
#include <queue>
#include <string>
using namespace std;
int f1(string file);
int f2(string file);
int f3(string file);
int f4(string file);
int sendpacket(string echoString);
int i1=0, i2=0, i3=0, i4=0, i5=0;
int kf(void){
return i1+i2+i3+i4+i5;
}
int main(int argc, char *argv[]){
/*
cout << "How many times do you want to run each function?" << endl;
int n;
cin >> n;
*/
typedef int (*ptof)(string);
int n = 1000;
timeval begin, end, tf1, tf2, tf3, tf4, tf5;
//Setting up queue of functions
deque<ptof> functions;
functions.push_back(f1);
functions.push_back(f2);
functions.push_back(f3);
functions.push_back(f4);
functions.push_back(sendpacket);
map<timeval, ptof> times;
times.insert(tf1,f1);
times.insert(tf2,f2);
times.insert(tf3,f3);
times.insert(tf4,f4);
times.insert(tf5,sendpacket);
map<int,ptof> exec;
exec.insert(i1,f1);
exec.insert(i2,f2);
exec.insert(i2,f2);
exec.insert(i3,f3);
exec.insert(i4,f4);
exec.insert(i5,sendpacket);
map<int,ptof> startTimes;
startTimes.insert(1000, f1);
startTimes.insert(500, f2);
startTimes.insert(150, f3);
startTimes.insert(50, f4);
startTimes.insert(0, sendpacket);
//beginning scheduling
gettimeofday(&begin, NULL);
ptof p;
int k=0;
while(k<=5000){
p = functions.pop_front();
if(k>startTimes[p]){
//Scheduler Code
}
k = kf();
}
gettimeofday(&end, NULL);
double t1=begin.tv_sec+(begin.tv_usec/1000000.0);
double t2=end.tv_sec+(end.tv_usec/1000000.0);
double elapsed = t2-t1;
cout << elapsed << " seconds elapsed\n" << endl;
return 0;
}
Но этот код дает мне ошибки, которые я не могу расшифровать, например:
../simpleRR.cpp:39: error: no matching function for call to 'std::map<timeval, int (*)(std::string), std::less<timeval>, std::allocator<std::pair<const timeval, int (*)(std::string)> > >::insert(timeval&, int (&)(std::string))'
/usr/include/c++/4.2.1/bits/stl_map.h:399: note: candidates are: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool>
И это действительно странно, это просто плохой подход?
../simpleRR.cpp:63: error: void value not ignored as it ought to be
../simpleRR.cpp:64: error: invalid conversion from 'int (*)(std::string)' to 'int'
../simpleRR.cpp:64: error: initializing argument 1 of '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int (*)(std::string), _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int (*)(std::string)> >]'
../simpleRR.cpp:64: error: ISO C++ forbids comparison between pointer and integer
Кто-нибудь может мне помочь?