代码之家  ›  专栏  ›  技术社区  ›  Kyle

如何修复这个充斥着模板的代码中的语法?

  •  2
  • Kyle  · 技术社区  · 15 年前

    template<typename T, typename U> class Alpha
    {
    public:
        template<typename V> void foo() {}
    };
    
    template<typename T, typename U> class Beta
    {
    public:
        Alpha<T, U> alpha;
        void arf();
    };
    
    template<typename T, typename U> void Beta<T, U>::arf()
    {
        alpha.foo<int>();
    }
    
    int main()
    {
        Beta<int, float> beta;
        beta.arf();
        return 0;
    }
    

    无法编译,原因是:

    ../src/main.cpp:在成员函数中 void Beta::arf():
    ../src/main。cpp:16:错误:应为 int之前的主表达式
    ../src/main。cpp:16:错误:应为 ; int之前

    我该怎么修?我想尽一切办法都试过了。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Georg Fritzsche    15 年前

    alpha::foo 是一个 dependent name ,使用 alpha.template foo<int>() .

    • 除非加前缀,否则不能是类型 typename
    • 除非直接以 template
        2
  •  4
  •   Alexandre C.    15 年前

    尝试 alpha.template foo<int>()