C ++模板编译错误:预期前主-EX pression'>'令牌 [英] C++ template compilation error: expected primary-expression before ‘>’ token

查看:186
本文介绍了C ++模板编译错误:预期前主-EX pression'>'令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code编译和预期(它会抛出运行时,不过没关系)的作品:

This code compiles and works as expected (it throws at runtime, but never mind):

#include <iostream>
#include <boost/property_tree/ptree.hpp>

void foo(boost::property_tree::ptree &pt) 
{
    std::cout << pt.get<std::string>("path"); // <---
}

int main()
{
    boost::property_tree::ptree pt;
    foo(pt);
    return 0;
}

但只要我添加模板,并修改原型转化

template<class ptree>
void foo(ptree &pt)

我得到了海湾合作委员会的错误:

I get an error in GCC:

test_ptree.cpp: In function ‘void foo(ptree&)’:
test_ptree.cpp:7: error: expected primary-expression before ‘>’ token

但随着MSVC ++没有错误!该错误是在标记行&LT; --- 。再次,如果我改变这个问题行成

but no errors with MSVC++! The error is in the marked line <---. And again, if I change the problem line into

--- std::cout << pt.get<std::string>("path"); // <---
+++ std::cout << pt.get("path", "default value");

错误消失(这个问题是在明​​确的&LT;标准::字符串&GT; )。

Boost.PropertyTree 需要升压> = 1.41。请帮助我了解和修复这个错误。

Boost.PropertyTree requires Boost >= 1.41. Please help me to understand and fix this error.

查看<一个href=\"http://stackoverflow.com/questions/1682844/templates-template-function-not-playing-well-with-classs-template-member-functi\">Templates:模板函数与类的模板成员函数打得很好 - 包含其他很好的答案和解释了类似的流行问题

See Templates: template function not playing well with class’s template member function — a similar popular question containing other good answers and explanations.

推荐答案

您需要做的:

std::cout << pt.template get<std::string>("path");

使用模板在相同的情况下为类型名称,除了模板的成员,而不是类型。

Use template in the same situation as typename, except for template members instead of types.

(也就是说,因为 PT :: GET 是一个模板成员的依赖的模板参数,你需要告诉它是一个编译器模板。)

(That is, since pt::get is a template member dependent on a template parameter, you need to tell the compiler it's a template.)

这篇关于C ++模板编译错误:预期前主-EX pression'&GT;'令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆