首页 > 编程语言 > C/C++ > 如何解决代码中存在模板函数时编译出错的问题:error: expected primary-expression before ‘>’ token
2019
06-20

如何解决代码中存在模板函数时编译出错的问题:error: expected primary-expression before ‘>’ token

在Ubuntu中编译C++项目时,其中GCC版本是7.4.0,调用模板函数的地方会报如下错误

error: expected primary-expression before ‘>’ token
this->pCmd = this->pBuffer->constructTail<CmdT>();

error: expected primary-expression before ‘)’ token
this->pCmd = this->pBuffer->constructTail<CmdT>();

error: expected primary-expression before ‘>’ token
this->pCmd = this->buffer.constructTail<CmdT>();

error: expected primary-expression before ‘)’ token
this->pCmd = this->buffer.constructTail<CmdT>();

问题原因:在调用模板函数时,解析器无法分辨>是一个模板参数的起始符号还是一个比较操作符;所以需要使用关键词告诉解析器这是一个模板参数的起始符号

解决方法:

this-&gt;pCmd = this-&gt;pBuffer-&gt;template constructTail();
this-&gt;pCmd = this-&gt;pBuffer-&gt;template constructTail();
this-&gt;pCmd = this-&gt;buffer.template constructTail();
this-&gt;pCmd = this-&gt;buffer.template constructTail();

 

最后编辑:
作者:游戏创作者大陆

留下一个回复

你的email不会被公开。