lundi 26 septembre 2016

Getting "‘int (*)(const std::vector

I'm trying out function pointers for the first time, and I'm getting an error while compiling.

void printResults ( int & inputSize )
{
 const vector<int> vec = createVector(inputSize);
 int (*alg1)(vector<int>) = &algorithm1;
 int (*alg2)(vector<int>) = &algorithm2;
 double runtime1 = calcTime( vec, (*alg1));
 double runtime2 = calcTime( vec, (*alg2));

 cout << "For an input size of " << inputSize << "," << endl;
 cout << "the run time of algorithm1 is " << runtime1 << endl;
 cout << "and the run time of algorithm2 is " << runtime2 << endl;
}

The functions algorithm1 and algorithm2 have the same output and I am measuring their runtime. The function calcTime takes a vector filled with random values from createVector as well as one of the algorithms to compute their run time.

The errors are

$ error: invalid conversion from ‘int (*)(const std::vector<int>&)’ to ‘int (*)(std::vector<int>)’ [-fpermissive]
  int (*alg1)(vector<int>) = &algorithm1;


'$ error: invalid conversion from ‘int (*)(const std::vector<int>&)’ to ‘int (*)(std::vector<int>)’ [-fpermissive]
   int (*alg2)(vector<int>) = &algorithm2;'

I have tried removing and putting const back in before every use of vector<int> to try to resolve the issue, which appears to be the most obvious solution, but it's doing nothing but creating more errors. Each resolution I've seen online isn't applicable to my code. Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire