gpt4 book ai didi

c++ - 模板参数无效

转载 作者:行者123 更新时间:2023-11-28 04:58:21 25 4
gpt4 key购买 nike

我正在尝试掌握模板并使用以下代码:

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 */

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 */

我目前没有 IDE,所以我通过运行来验证正确性

$ g++ flat_town.h

这会产生以下错误:

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>> {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我对这些高阶模板感到非常不确定,感谢所有帮助。

问候,托拜厄斯

最佳答案

您错误地指定了模板模板参数。只需指定模板的名称,即

class FlatTown: public Town<Pos, Resident, Neighbourhood> {

关于c++ - 模板参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46681617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com