g++ error: expected primary-expression

北战南征 提交于 2019-12-13 21:22:21

问题


look at this sample:

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();
}

The code compile with the following error:

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

When i compiled the code with visual c++ 2008 and Intel C++ it compiles without problem.

What that error mean?


回答1:


You need the template qualifier:

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

See:

Where and why do I have to put the "template" and "typename" keywords?




回答2:


just add

template

keyword:

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


来源:https://stackoverflow.com/questions/15792462/g-error-expected-primary-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!