gre*_*fab 3 c++ templates compiler-errors
当我尝试编译此代码时:
struct BasicVertexProperties
{
Vect3Df position;
};
struct BasicEdgeProperties
{
};
template < typename VERTEXPROPERTIES, typename EDGEPROPERTIES >
class Graph
{
typedef adjacency_list<
setS, // disallow parallel edges
vecS, // vertex container
bidirectionalS, // directed graph
property<vertex_properties_t, VERTEXPROPERTIES>,
property<edge_properties_t, EDGEPROPERTIES>
> GraphContainer;
typedef graph_traits<GraphContainer>::vertex_descriptor Vertex;
typedef graph_traits<GraphContainer>::edge_descriptor Edge;
};
Run Code Online (Sandbox Code Playgroud)
g ++抱怨"typedef graph_traits <>"行中出现以下错误:
error: type 'boost::graph_traits<boost::adjacency_list<boost::setS, boost::vecS,
boost::bidirectionalS, boost::property<vertex_properties_t, VERTEXPROPERTIES,
boost::no_property>, boost::property<edge_properties_t, EDGEPROPERTIES,
boost::no_property>, boost::no_property, boost::listS> >' is not derived from type
'Graph<VERTEXPROPERTIES, EDGEPROPERTIES>'
Run Code Online (Sandbox Code Playgroud)
我发现编译器似乎不知道我的模板参数是类型,但在属性定义中将"typename"放在它们之前并没有帮助.
怎么了?我只是希望有一个模板化的Graph类,可以使用我喜欢的任何属性,从上面定义的基本属性结构派生,所以我可以在这个Graph中使用基本属性的方法.
这些线:
typedef graph_traits<GraphContainer>::vertex_descriptor Vertex;
typedef graph_traits<GraphContainer>::edge_descriptor Edge;
Run Code Online (Sandbox Code Playgroud)
应该:
typedef typename graph_traits<GraphContainer>::vertex_descriptor Vertex;
typedef typename graph_traits<GraphContainer>::edge_descriptor Edge;
Run Code Online (Sandbox Code Playgroud)
原因是编译器不能告诉vertex_descriptor是一个类型,直到你定义GraphContainer是什么(因为一个可能用另一个来定义).
此标准要求您指定这是一个类型而不是静态成员变量.