我正在尝试创建一个扩展boost图库行为的类。我希望我的类成为一个模板,用户提供一个类型(类),用于存储每个顶点的属性。那只是背景。我正在努力创建一个更简洁的typedef来用来定义我的新类。
基于this和this等其他帖子,我决定定义一个包含模板化typedef的结构。
我将展示两种密切相关的方法。我无法弄清楚为什么GraphType的第一个typedef似乎正在工作,而VertexType的第二个类型失败。
#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
我该如何解决这个问题?
答案 0 :(得分:5)
typedef typename boost::graph_traits<GraphType>::vertex_descriptor VertexType;
// ^^^^^^^^
应该修理它,我不知道你试图把它放在哪里..你可能还有其他问题我看不到。