lundi 11 novembre 2019

Class template unrecognizable template

I have tried searching but there does not seem to be a similar question.

I am trying to implement a class template like this:

Individual.h:

template <typename GeneType>
class Individual
{
private:
    Fitness fitness;
    GeneType genes;

public:
    Individual(Fitness fitness, GeneType genes);
};

Individual.cpp:

#include "Individual.h"

template <typename GeneType>
Individual<GeneType>::Individual(Fitness fitness, GeneType genes) : fitness(fitness), genes(genes) {}

The problem is that I get:

error C2988: unrecognizable template declaration/definition
1>d:\projects\visualstudio projects\imageprocessing\imageprocessing\individual.cpp(8): error C2059: syntax error: '('
1>d:\projects\visualstudio projects\imageprocessing\imageprocessing\individual.cpp(8): error C2065: 'fitness': undeclared identifier
1>d:\projects\visualstudio projects\imageprocessing\imageprocessing\individual.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\projects\visualstudio projects\imageprocessing\imageprocessing\individual.cpp(8): error C2065: 'genes': undeclared identifier
1>d:\projects\visualstudio projects\imageprocessing\imageprocessing\individual.cpp(8): error C2448: 'genes': function-style initializer appears to be a function definition

If I comment out the implementation of the constructor the other trivial accessors in the class compile without issue. The accessors look like this:

template<typename GeneType>
Fitness& Individual<GeneType>::Fitness() { return fitness; } 

My question is why does that error pop up and what is the correct way to do it?

Aucun commentaire:

Enregistrer un commentaire