2

我在某些 g++ 4.x 版本上正确编译了此代码,现在 4.6 版终止编译并出现错误:

/usr/include/boost/pending/property.hpp:35:7: error: ‘boost::property<Tag, T, Base>::m_value’ has incomplete type

该错误似乎是由 graph_type 和 edge_info 类型声明之间的循环引起的。

我能够在以下几行代码中隔离问题。如何使用依赖于节点的捆绑属性的类型来定义边的属性?该解决方案仍应使用捆绑属性(因为很多代码取决于此图形类型)。如何更正以下代码?

#include <iostream>
#include <boost/graph/adjacency_list.hpp>

using namespace std;
using namespace boost;

template<typename map_type>
struct map_computation {
  map_type m;
};

struct vertex_info;
struct edge_info;

typedef adjacency_list<vecS, 
               vecS, 
               bidirectionalS, 
               vertex_info, 
               edge_info> graph_type;

struct position {
  double x, y;
};

struct vertex_info {
  position p;
};

typedef boost::property_map<graph_type, 
                position vertex_info::*>::type position_map_type;

struct edge_info {
  map_computation<position_map_type>* c;
};

int main(int argc, char* argv[])
{
  graph_type g;
  return 0;
}

编辑:完整的错误日志如下:

In file included from /usr/include/boost/graph/graph_traits.hpp:22:0,
                 from /usr/include/boost/graph/adjacency_list.hpp:33,
                 from gtest.cc:2:
/usr/include/boost/pending/property.hpp: In instantiation of ‘boost::property<boost::edge_bundle_t, edge_info, boost::no_property>’:
/usr/include/boost/pending/detail/property.hpp:94:48:   instantiated from ‘boost::detail::build_property_tag_value_alist<boost::property<boost::edge_bundle_t, edge_info, boost::no_property> >’
/usr/include/boost/pending/property.hpp:63:81:   instantiated from ‘boost::property_value<boost::property<boost::edge_bundle_t, edge_info, boost::no_property>, boost::edge_bundle_t>’
/usr/include/boost/graph/properties.hpp:448:63:   instantiated from ‘boost::graph_detail::retagged_bundle<boost::property<boost::edge_bundle_t, edge_info, boost::no_property>, boost::edge_bundle_t>’
/usr/include/boost/graph/properties.hpp:461:64:   instantiated from ‘boost::graph_detail::normal_property<edge_info, boost::edge_bundle_t>’
/usr/include/boost/graph/properties.hpp:473:12:   instantiated from ‘boost::graph_detail::edge_prop<edge_info>’
/usr/include/boost/graph/adjacency_list.hpp:381:70:   instantiated from ‘boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, vertex_info, edge_info>’
/usr/include/boost/graph/properties.hpp:418:44:   instantiated from ‘boost::property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, vertex_info, edge_info>, position vertex_info::*>’
gtest.cc:30:32:   instantiated from here
/usr/include/boost/pending/property.hpp:35:7: error: ‘boost::property<Tag, T, Base>::m_value’ has incomplete type
gtest.cc:13:8: error: forward declaration of ‘struct edge_info’
gtest.cc:33:45: error: template argument 1 is invalid
4

1 回答 1

1

你不能有一个递归类型定义,比如

struct edge_info;

typedef adjacency_list<..., edge_info> graph_type;

typedef boost::property_map<graph_type, ...>::type position_map_type;

struct edge_info { map_computation<position_map_type>* c; };

正如例如这个问题中提到的,C++ 标准在 §17.4.3.6/2 中说,

特别是,在以下情况下效果是不确定的:

__ [..] — 如果在实例化模板组件时将不完整类型 (3.9) 用作模板参数。__ [..]

根据您想要完成的任务(从您的问题中并不完全清楚),您可能需要考虑 Curiously Recurring Template Pattern (CRTP),您可以在其中执行以下操作:

template<typename edge>
class some_graph_type 
{ // ... };

class my_edge_type
: 
    public some_graph_type<my_edge_type> 
{ // ... };

因此,您可以让一个edge类从一个类模板派生,并将其自身作为模板参数。类似地,您可以让一个edge类有一个edge*成员(类似于链表),但没有一个您需要首先了解其edge自身完整定义的成员。

于 2012-06-11T08:36:12.210 回答