簡體   English   中英

Boost graph typedef c ++的struct的前向聲明

[英]Forward declaration of struct for Boost graph typedef c++

簡短的問題描述:

基本上我想要

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,但那個當然沒有初始化。 如何解決此循環引用?

問題是,在實例化和定義until container<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;
}

你可以參考我什么時候可以使用前向聲明? 有關前瞻性聲明的更多詳情......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM