面向Boost图形类型的前向结构声明
Forward declaration of struct for Boost graph typedef c++
简短的问题描述:
基本上我想要
struct Type;
typedef container<Type> MyType;
struct Type{
MyType::sometype member;
}
我该怎么做呢?
实际问题:对于Boost连续最短路径算法,我需要将我的正向边映射到它们的反向边。我有以下代码:
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的完整类型,但那个当然没有初始化。我如何解决这个循环引用?
问题是struct Type
不能被定义(直到大小和内存布局)until container<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?
相关文章:
- 为什么 C++ 中的以下结构声明会导致退出 127?
- 在.cpp文件之间传递结构声明,而不使用全局头文件
- 结构声明中的 if 语句C++
- 这个嵌套结构声明是什么意思?
- 在结构声明的分号之前声明的结构变量是否为全局变量
- 为什么结构声明在C++中违反了 ODR
- 错误:多个文件中的结构声明冲突
- 为什么调用定义的构造函数会导致结构声明中出现错误,我该如何修复它
- C++数据结构声明
- 相互依赖的结构声明
- 结构声明的数组
- 结构声明中的":"是什么意思
- 面向C++的main.cpp中的可视化结构声明问题
- 当某些结构字段被省略或与结构声明中的顺序不同时,如何实现正确的解析?
- 编译器在转发结构声明上出错
- C++生成器 XE4 中的结构声明
- 我无法理解c ++中的结构声明
- 嵌套结构声明以及如何访问它们
- 将包含单个数组的结构声明为packed有什么缺点吗
- 定义之前的结构声明