1

在尝试将一些 boost 图形代码重构为函数模板时,我遇到了一些编译错误。

我正在尝试将该using NODE_TYPE = int;行移动到函数模板参数中。

我究竟做错了什么?

编译错误

Test_Dijkstra_Graph.cpp:48:28: error: expected type-specifier
Test_Dijkstra_Graph.cpp:48:28: error: expected ‘;’
Test_Dijkstra_Graph.cpp:49:28: error: expected type-specifier
Test_Dijkstra_Graph.cpp:49:28: error: expected ‘;’

旧代码编译得很好

void 
find_shortest_paths_by_pairs()
{
  using NODE_TYPE         = int;
  using EDGE              = std::pair<NODE_TYPE,NODE_TYPE>;
  using GRAPH_T           = adjacency_list< 
        listS, vecS, directedS, no_property, property< edge_weight_t, NODE_TYPE >>;
  using vertex_descriptor = graph_traits<GRAPH_T>::vertex_descriptor;
  using edge_descriptor   = graph_traits<GRAPH_T>::edge_descriptor;
  // ...
}

新代码无法编译

template<typename NODE_TYPE>
void find_shortest_paths_by_pairs()
{
  using EDGE              = std::pair<NODE_TYPE,NODE_TYPE>;
  using GRAPH_T           = adjacency_list< 
        listS, vecS, directedS, no_property, property< edge_weight_t, NODE_TYPE >>;
  using vertex_descriptor = graph_traits<GRAPH_T>::vertex_descriptor;   // ERROR HERE
  using edge_descriptor   = graph_traits<GRAPH_T>::edge_descriptor;     // ERROR HERE
  // ...
}

// find_shortest_paths_by_pairs<int>();
4

1 回答 1

5

NODE_TYPE原因GRAPH_T是一个依赖类型,所以你需要几个typenames:

using vertex_descriptor = typename graph_traits<GRAPH_T>::vertex_descriptor;
using edge_descriptor   = typename graph_traits<GRAPH_T>::edge_descriptor;

(Comeau 有一个常见问题解答,我通常会链接到这里,详细说明为什么这是必要的,但似乎他们已经让他们的域名过期了。:-[)

编辑:嘿,Comeau 的域名又回来了。说常见问题:http: //www.comeaucomputing.com/techtalk/templates/#typename

于 2013-01-27T02:03:03.997 回答