编译器错误:在'>'标记之前的预期primary-expression

Ash*_*hot 2 c++ qt compiler-errors

#include <string>
...
template <typename DefinitionsIterator>
void parse(const CIET_NS ::VariadicArguments& argumentList, DefinitionsIterator firstDef, DefinitionsIterator lastDef, Map& res)
{
    for (int i = 0; i < argumentList.size(); ++i) {
        CIET_NS ::Object obj = argumentList.at(i);
        std::string objStr = obj.convert<std::string>();
        qDebug() << objStr.c_str();

        //qDebug() << argumentList.at(i).convert<std::string>().c_str();

    }
Run Code Online (Sandbox Code Playgroud)

此代码编译但注释的行没有.我收到了这个错误

 error: expected primary-expression before '>' token
Run Code Online (Sandbox Code Playgroud)

怎么会发生这种情况?

template <typename ChildClass, typename ListElementType, typename DuplicateType>
class BasicObject
{
public:
    BasicObject();
    ~BasicObject();

public:
    Tcl_Obj* tclObject() const;
    Tcl_Obj* releaseObject();
    template <typename T>
    T convert(Interpreter& interp) const;
    template <typename T>
    T convert() const;
Run Code Online (Sandbox Code Playgroud)

Object 源于 BasicObject

Compiler version:
g++ -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)
Run Code Online (Sandbox Code Playgroud)

Bo *_*son 8

何时convert是模板,您必须指明(类似于typename用于表示名称是类型).

qDebug() << argumentList.at(i).template convert<std::string>().c_str();
                               ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

否则编译器认为这<是一个比较,并且在看到>可以比较的东西之前会感到困惑.

  • 但为什么要用三行编译呢? (2认同)