繁体   English   中英

g ++错误:预期的主表达式

[英]g++ error: expected primary-expression

看这个例子:

struct parent
{
    template <typename t>
    inline static t get_t();
};

struct child : public parent
{
    template <typename t>
    inline static t get_t()
    {
        return t(-1);
    }
};

template <typename t>
struct call
{
    inline static int get_value()
    {
        return t::get_t<int>();
    }
};

typedef call< child > test;

int main()
{
    int v = test::get_value();
}

该代码编译时出现以下错误:

In static member function 'static int call<t>::get_value()':
  error: expected primary-expression before 'int'
  error: expected ';' before 'int'
  error: expected unqualified-id before '>' token

当我使用Visual c ++ 2008和Intel C ++编译代码时,它可以毫无问题地进行编译。

该错误是什么意思?

您需要模板限定符:

return t::template get_t<int>();

看到:

为什么必须在何处以及为什么要放置“模板”和“类型名”关键字?

只需添加

模板

关键词:

template <typename t>
struct call
{
    inline static int get_value()
    {
        return t::template get_t<int>();
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM