I have a strange problem, I have a Fitness
class (Fitness.h
+Fitness.cpp
) that is used by another class Individual
:
Individual.h
#pragma once
#include "Fitness.h"
template <typename GeneType>
class Individual
{
private:
Fitness fitness;
GeneType genes;
public:
Individual(Fitness fitness, GeneType genes);
Fitness& Fitness();
GeneType& Genes();
Individual<GeneType> copy();
};
Individual.cpp
#include "stdafx.h"
#include "Individual.h"
// Ctors
template <typename GeneType>
Individual<GeneType>::Individual(Fitness fitness, GeneType genes) {} // line 8
// Fitness
template<typename GeneType>
Fitness& Individual<GeneType>::Fitness() { return fitness; }
// Genes
template<typename GeneType>
GeneType& Individual<GeneType>::Genes() { return genes; }
// Copy
template<typename GeneType>
Individual<GeneType> Individual<GeneType>::copy() { return Individual<GeneType>(fitness, genes); }
When building the solution I get:
...\individual.cpp(8): error C2988: unrecognizable template declaration/definition
...\individual.cpp(8): error C2059: syntax error: '('
...\individual.cpp(8): error C2143: syntax error: missing ';' before '{'
...\individual.cpp(8): error C2447: '{': missing function header (old-style formal list?)
The errors point to a problem in declaring the ctor but it seems fine. After trying different things I have found out that if I name the fitness
accessor something else like GetFitness
instead of just Fitness
it works fine, which led me to believe there is some kind of conflict.
But I have also read that a popular naming convention (Google?) uses CamelCased names for accessors so my_var
uses MyVar()
as the accessor but at the same time using the lowercase class name for a member variable is something fairly common, so in turn using an accessor that just so happens to have the same name as the class also looks like it could be a common problem.
Bottom line is: Why does this happen? In my experience with other languages the method names (Fitness()
) for a class would be in a whole different namespace than a "neighboring" class (class Fitness
) which would be (in absence of an explicit namespace) in the global namespace.
Also, is there a way to make this code play nice without changing the naming convention for accessors?