-
Syntax Help Please
I'm playing around with genetic algorithms, and like always, i'm trying to templatize part of it. Although I'll probably abandon this code because I don't like where it's going, I'm still curious as to how I can get it to compile.
The problem I'm having is passing the template argument to "float fitness_score(Chromo<>::DnaType)". As is, i get the error:
37 C:\compilers\bloodshed\main.cpp invalid conversion from `float (*)(Chromo<32>::DnaType)' to `float (*)(Chromo<16>::DnaType)'
I've tried just about all I know, but I can't seem to get it to compile.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
template<int FieldSize = 32>
class Chromo
{
public:
struct DnaType{ unsigned dt_dna: FieldSize; };
Chromo(void) : geneCount(FieldSize){}
Chromo(const Chromo<> &s) : geneCount(s.geneCount)
{dna = s.dna;}
float score(float (*sc)(DnaType));
private:
unsigned dna : FieldSize;
const int geneCount;
};
float fitness_score(Chromo<>::DnaType dt)
{
//dummy function
return 1.2f;
}
int main(int argc, char *argv[])
{
const int x = 16;
Chromo<x> ccc;
cout << ccc.score(fitness_score) << endl;
system("PAUSE"); //dev-c++ generated
return EXIT_SUCCESS;
}
Any ideas?
-
Well, the class has a default template argument of 32, so the argument to fitness_score() will always be 32, since it uses the default argument ( <> ). ccc on the other hand is initialized to use 16. These two are, of course, not compatible.
Perhaps ypou want to make your fitness_score a templated function?
-
i have tried, but i can't seem to find the right syntax for it
-
Just like any templated thingy:
Code:
template<int FieldSize = 32>
float fitness_score(Chromo<FieldSize>::DnaType dt)
{
//dummy function
return 1.2f;
}
...unless my memory fails me...