【发布时间】:2011-06-20 15:15:12
【问题描述】:
我正在尝试创建一个扩展 boost 图形库行为的类。我希望我的类是一个模板,用户提供一个类型(类),用于在每个顶点存储属性。这只是背景。我正在努力创建一个更简洁的 typedef 以用于定义我的新类。
根据this 和this 等其他帖子,我决定定义一个包含模板化类型定义的结构。
我将展示两种密切相关的方法。我不明白为什么 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