c++ - 为什么在 graph_traits<> 中使用模板化类型定义时 g++ 会报错?

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

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>'

我发现编译器似乎不知道我的模板参数是类型,但在属性定义中将“typename”放在它们之前没有帮助。

怎么了?我只是想要一个模板化的 Graph 类,以便能够使用我喜欢的任何属性,派生自上面定义的基本属性结构,这样我就可以在这个 Graph 中拥有对基本属性进行操作的方法。

最佳答案

这些行:

    typedef graph_traits<GraphContainer>::vertex_descriptor Vertex;
    typedef graph_traits<GraphContainer>::edge_descriptor Edge;

应该是:

    typedef typename graph_traits<GraphContainer>::vertex_descriptor Vertex;
    typedef typename graph_traits<GraphContainer>::edge_descriptor Edge;

原因是编译器无法判断 vertex_descriptor 是一种类型,直到您定义 GraphContainer 是什么(因为一个可能根据另一个定义)。

此标准要求您指定这是一个类型而不是静态成员变量。

关于c++ - 为什么在 graph_traits<> 中使用模板化类型定义时 g++ 会报错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/883999/

相关文章:

java - 即使有一个 main 方法,也无法找到或加载主类

c++ - 使用 GCC 4.7 从初始化程序列表初始化 unique_ptrs 的容器失败

c++ - 919B |第n个数字的总和为10 |代码部队

c++ - 如何构建具有特定签名的函数概念?

c++ - 使用可变参数模板解决 C++ 中的 mixin 构造函数问题

templates - 如何获取Joomla模板样式名称?

c - 对我有一个库的东西的 undefined reference

c++ - DirectX 10 IDXGISwapChain::GetBuffer 多线程 CTD 错误

c++ - 成对 vector 和包含两个元素的结构 vector 的内存布局差异 - C++/STL

c++ - 'new' 在相对较小的分配上导致 std::bad_alloc