【问题标题】:Strange template template parameter expected error [duplicate]奇怪的模板模板参数预期错误[重复]
【发布时间】:2015-01-24 14:38:38
【问题描述】:

当试图编译这段代码时:

template <class URNG>
struct Dumb : Brain<Dumb, URNG>
{
    Move operator()(const Rat<Dumb, URNG>& rat, URNG&& urng)
    {
        Move move;
        move.x = 1;
        move.y = 0;
        //rat.look(1, 2);
        //rat.getDna(35);
        return move;
    }
};

clang 3.2.7 raise,这个奇怪的错误我不明白:

main.cpp:10:28: error: template argument for template template parameter must be a class template or type alias template
        Move operator()(const Rat<Dumb, URNG>& rat, URNG&& urng)
                                  ^

Dumb 是一个类模板不是吗?

按照 cmets 的要求,这是老鼠的样子:

template <template <class> class BRAIN, class URNG>
class Rat
{
//...
}

【问题讨论】:

  • Rat的定义是什么?
  • 我刚刚添加了老鼠模板的样子。
  • @πάνταῥεῖ 我想你可能是对的...... :)

标签: c++ templates c++11 template-templates


【解决方案1】:

您的问题是由于注入的名称:

template <class URNG>
struct Dumb : Brain<Dumb, URNG>
{
    // in here, "Dumb" refers to the complete type "Dumb<URNG>"
    Move operator()(const Rat<Dumb, URNG>& rat, URNG&& urng)
                          //  ^^^^ 
                          //  really a type, not a template

要解决这个问题,您需要引用未注入的名称,您可以这样做:

template <class URNG>
struct Dumb : Brain<Dumb, URNG>
{
    Move operator()(const Rat<::Dumb, URNG>& rat, URNG&& urng)
                          //  ^^^^^^
                          //  actual Dumb<T> template, not type

【讨论】:

  • 太棒了!谢谢 !没有想过在struct里面引用类型或者模板这个问题。
  • 这是一个编译器错误。当用作模板模板参数的参数时,注入的类名应被视为模板。
  • @T.C.啊,没有意识到 C++11 发生了变化。但是对于 C++03,拒绝它是正确的——而且不清楚 OP 是否使用 C++11。
  • @Barry URNG&amp;&amp;,提示,提示。 (对于 C++03,::Dumb 之前需要一个空格;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
  • 2016-01-20
  • 2021-02-07
  • 2011-03-20
  • 1970-01-01
  • 2012-04-30
相关资源
最近更新 更多