0

I'm trying to get a hang of templates and have the following code:

town.h

#ifndef TOWN_H
#define TOWN_H

namespace segregation {
    template <typename Pos, typename Resident, template <typename> class Container>
    class Town {
    public:
        Town();
        virtual ~Town();
        virtual Resident getResident(Pos address); 
        virtual void move(const Pos& from, const Pos& to);
        virtual Container<Resident> getNeighbours(const Pos address);
    };
}


#endif /* TOWN_H */

and

flat_town.h

#ifndef FLAT_TOWN_H
#define FLAT_TOWN_H
#include "town.h"
#include <array>
#include <forward_list>
#include <utility>
namespace segregation {

    template<int x, int y, class R>
    using TownMap = std::array<std::array<R, y>, x>;

    template <typename R>
    using Neighbourhood = std::forward_list<R>;

    using Pos = std::pair<int, int>;

    template <int x, int y, class Resident>
    class FlatTown: public Town<Pos, Resident, Neighbourhood<typename Resident>> {
    private:
        TownMap<x, y, Resident> m_townMap;
        int m_neighbourhood_size;

    public:
        FlatTown(TownMap<x, y, Resident> t_townMap, int t_neighbourhood_size) :
            m_townMap(t_townMap), m_neighbourhood_size(t_neighbourhood_size) {};

        Resident getResident(const Pos & address);

        void move(const Pos & from, const Pos & to);

        Neighbourhood<Resident> getNeighbours(const Pos & address);

        virtual ~FlatTown();
    };
}

#endif /* FLAT_TOWN_H */

I don't currently have an IDE so I am verifying correctness by running

$ g++ flat_town.h

Which generates the following error:

flat_town.h:18:76: error: template argument 1 is invalid
  class FlatTown: public Town<Pos, Resident, Neighbourhood<typename Resident>> {
                                                                            ^~
flat_town.h:18:45: error: template argument 3 is invalid
  class FlatTown: public Town<Pos, Resident, Neighbourhood<typename Resident>> {
                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I feel very uncertain about these higher order templates and all help is appreciated.

Regards, Tobias

user25470
  • 585
  • 4
  • 17
  • 1
    I'm more interested in why you compile a header file? Why not the source file which *includes* the header file? – Some programmer dude Oct 11 '17 at 06:55
  • You should probably also read [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) – Some programmer dude Oct 11 '17 at 06:57
  • 1
    @Someprogrammerdude in a sense it's the natural way to get at a minimal sample (it's just chopping an unnecessary `#include "flat_town.h"` :)) – sehe Oct 11 '17 at 06:57
  • @Someprogrammerdude I'm just trying to get rid of bugs one step at a time, since this one was related to the template it seemed natural to restrict g++ to the header. I will read the link you referenced, thanks – user25470 Oct 11 '17 at 07:16

1 Answers1

3

You're specifying the template template argument incorrectly. Just specify the name of the template, i.e.

class FlatTown: public Town<Pos, Resident, Neighbourhood> {
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Of course! thank you! By the way, I'm coming from scala and I am not sure that I am doing c++ correctly. Do you know if this kind of generic programming is the way to go? – user25470 Oct 11 '17 at 06:59
  • @user25470 It's hard to say that without knowing the use scenario. Anyway, the current codes seem fine. – songyuanyao Oct 11 '17 at 07:04
  • The idea was to simulate movements within a city to see if segregation occurs naturally given some similarity preference among the residents, i.e. a resident of type A will leave a neighbourhood for an other if its more in line with its preferences. Since I want to experiment a little i wanted the base, town.h, to be as generic as possible – user25470 Oct 11 '17 at 07:17