加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Boost graph typedef c的struct的前向声明

发布时间:2020-12-16 07:08:58 所属栏目:百科 来源:网络整理
导读:简短的问题描述: 基本上我想要 struct Type;typedef containerType MyType;struct Type{ MyType::sometype member;} 现在,我该怎么做? 实际问题: 对于Boost Succesive Shortest Path算法,我需要将前向边缘映射到它们的反向.我有以下代码: struct VertexP
简短的问题描述:

基本上我想要

struct Type;
typedef container<Type> MyType;
struct Type{
    MyType::sometype member;
}

现在,我该怎么做?

实际问题:

对于Boost Succesive Shortest Path算法,我需要将前向边缘映射到它们的反向.我有以下代码:

struct VertexProperty { };
struct EdgeProperty;
typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::directedS,VertexProperty,EdgeProperty> DirectedGraph;

struct EdgeProperty {
    double edge_capacity; //capacity: 1 for forward,0 for reverse
    double edge_weight; //cost
    DirectedGraph::edge_descriptor reverse_edge; //reverse edge mapping

    //forward edge constructor:
    EdgeProperty(double distance,DirectedGraph::edge_descriptor reverseEdge) :
            edge_capacity(1),edge_weight(distance),reverse_edge(reverseEdge) {
    };

    //reverse edge constructor
    EdgeProperty(double distance) :
            edge_capacity(0),edge_weight(-distance) {
    };

};

但是,现在我收到以下错误:

/usr/include/boost/pending/property.hpp:35:7: error: ‘boost::property<Tag,T,Base>::m_value’ has incomplete type
../src/Tester.cpp:21:8: error: forward declaration of ‘struct EdgeProperty’

我想这是有道理的:对于DirectedGraph :: edge_descriptor我需要完整类型的EdgeProperty,但那个当然没有初始化.如何解决此循环引用?

解决方法

问题是,在容器< Type>之前,不能定义struct Type(最大尺寸和内存布局).是实例化和定义的,但这取决于struct Type的定义….导致循环依赖.

使用指针或智能指针中断依赖关系,在指定其指向类型之前可以确定其大小和布局.

例如:

#include <vector>
#include <memory>

struct Type;
typedef std::vector<Type> MyType;
struct Type{
    std::shared_ptr<MyType::value_type> member;
};

int main() {
        Type t;
}

有关前向声明的更多详细信息,请参阅When can I use a forward declaration? …

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读