2

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?

Rares Dima
  • 1,575
  • 1
  • 15
  • 38
  • 1
    clang and gcc seem to disagree here: https://gcc.godbolt.org/z/zNnqXk – Vittorio Romeo Nov 11 '19 at 10:55
  • [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file?noredirect=1&lq=1) – Some programmer dude Nov 11 '19 at 11:00
  • 1
    `::Fitness fitness` should do it, if you really want this naming convention. – StoryTeller - Unslander Monica Nov 11 '19 at 11:01
  • @VittorioRomeo sorry, I use MSVC, should have mentioned. And using godbolt msvc for your example it seems to... give no error for some reason – Rares Dima Nov 11 '19 at 11:04
  • @Someprogrammerdude I have read that question but while informative I don't see how it answers the question. – Rares Dima Nov 11 '19 at 11:06
  • @StoryTeller-UnslanderMonica Worked if I put `::Fitness` as the type in the implementation of the constructor. But... why? What was the issue and what did `::Fitness` do? – Rares Dima Nov 11 '19 at 11:07
  • @RaresDima - Vittorio chose a good duplicate. It should describe the issue well enough. – StoryTeller - Unslander Monica Nov 11 '19 at 11:09
  • @RaresDima It doesn't which is why it's a comment and not an answer. But think about that and how you show us a template class in a header file, and its implementation in a separate source file. – Some programmer dude Nov 11 '19 at 11:12
  • @StoryTeller-UnslanderMonica it does describe the issue but that person was also told to just use `::Identifier`, it worked for him, and has not received an answer. Should I create a separate question for this? – Rares Dima Nov 11 '19 at 11:33

0 Answers0