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

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

问题描述

此代码按预期编译和工作(它在运行时抛出,但不介意):

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

但是一旦我添加模板并更改 foo 原型到

But as soon as I add templates and change the foo prototype into

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



我在GCC中遇到错误: p>

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 ++的错误!错误在标记行< --- 中。再次,如果我把问题行改成

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");

错误消失(问题在于显式< std :: string> ; )。

the error disappears (the problem is in explicit <std::string>).

Boost.PropertyTree 要求Boost> = 1.41。请帮助我理解并修复此错误。

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

请参阅模板:模板函数不能很好地与类的模板成员函数

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");

使用模板

(也就是说,由于 pt:code> typename :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 ++模板编译错误:'&gt;'令牌之前的预期主表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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