【问题标题】:Templated typedef soup using boost graph library使用 boost 图形库的模板化 typedef 汤
【发布时间】:2011-06-20 15:15:12
【问题描述】:

我正在尝试创建一个扩展 boost 图形库行为的类。我希望我的类是一个模板,用户提供一个类型(类),用于在每个顶点存储属性。这只是背景。我正在努力创建一个更简洁的 typedef 以用于定义我的新类。

根据thisthis 等其他帖子,我决定定义一个包含模板化类型定义的结构。

我将展示两种密切相关的方法。我不明白为什么 GraphType 的第一个 typedef 似乎正在工作,而 VertexType 的第二个 typedef 失败了。

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

template <class VP>
struct GraphTypes
{
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > GraphType;
    typedef boost::graph_traits< GraphType >::vertex_descriptor VertexType;
};

int main()
{
    GraphTypes<int>::GraphType aGraphInstance;
    GraphTypes<int>::VertexType aVertexInstance;
    return 0;
}

编译器输出:

$ g++ -I/Developer/boost graph_typedef.cpp 
graph_typedef.cpp:8: error: type ‘boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP, boost::no_property, boost::no_property, boost::listS> >’ is not derived from type ‘GraphTypes<VP>’
graph_typedef.cpp:8: error: expected ‘;’ before ‘VertexType’
graph_typedef.cpp: In function ‘int main()’:
graph_typedef.cpp:14: error: ‘VertexType’ is not a member of ‘GraphTypes<int>’
graph_typedef.cpp:14: error: expected `;' before ‘aVertexInstance’

同样的事情,只是避免在第二个 typedef 中使用GraphType

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

template <class VP>
struct GraphTypes
{
    typedef                      boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > GraphType;
    typedef boost::graph_traits< boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > >::vertex_descriptor VertexType;
};

int main()
{
    GraphTypes<int>::GraphType aGraphInstance;
    GraphTypes<int>::VertexType aVertexInstance;
    return 0;
}

编译器输出看起来实际上是一样的:

g++ -I/Developer/boost graph_typedef.cpp 
graph_typedef.cpp:8: error: type ‘boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP, boost::no_property, boost::no_property, boost::listS> >’ is not derived from type ‘GraphTypes<VP>’
graph_typedef.cpp:8: error: expected ‘;’ before ‘VertexType’
graph_typedef.cpp: In function ‘int main()’:
graph_typedef.cpp:14: error: ‘VertexType’ is not a member of ‘GraphTypes<int>’
graph_typedef.cpp:14: error: expected `;' before ‘aVertexInstance’

显然,第一个编译器错误是根本问题。我尝试在几个地方插入typename,但没有成功。我正在使用 gcc 4.2.1

我该如何解决这个问题?

【问题讨论】:

    标签: c++ templates boost typedef


    【解决方案1】:
    typedef typename boost::graph_traits<GraphType>::vertex_descriptor VertexType;
    //      ^^^^^^^^
    

    应该修复它,但我不知道你试图把它放在哪里.. 你可能还有其他我看不到的问题。

    【讨论】:

    • 哦,天哪,是的。那将是我认为抛出类型名令牌的最后一个地方。当然 typedef 关键字后面的第一个参数应该是类型名。
    • @Noah:准确地说,它必须直接在依赖名称之前(嵌套 typedef 或类/结构)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2011-02-13
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    相关资源
    最近更新 更多