【发布时间】:2020-03-17 23:28:39
【问题描述】:
在 MyGraphBuilder 类中,我在执行 Graph 属性分配期间遇到了一些问题:
G:\QT\Projects\My Project\boost\graph\detail\adjacency_list.hpp:2700: error: forming reference to void typedef value_type& reference;G:\QT\Projects\MY Project\boost\graph\dijkstra_shortest_paths.hpp:588: error: no matching function for call to 'choose_const_pmap(const type&, const boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertProperty, EdgeProperty>&, boost::edge_weight_t)' choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
另外,当我使用打印功能只打印节点和边的计数时,它只计算节点,但边的计数显示为“0”,请问有什么问题吗?
头文件:
#ifndef MYGRAPHBUILDER_H
#define MYGRAPHBUILDER_H
// Generic Libraries
//===============================================
#include <iostream>
#include <vector>
#include <map>
// Boost Libraries
//===============================================
#include <boost/config.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graph_utility.hpp>
// MyColleague Libraries
//===============================================
#include <modelDataStructure.h>
#include <modelDataHandler.h>
#include <modeldata.h>
#include <model.h>
// Osmium Libraries
//===============================================
#include <osmium/osm.hpp>
#include <osmium/index/map/flex_mem.hpp>
#include <osmium/visitor.hpp>
#include <osmium/osm/location.hpp>
#include <osmium/osm/node_ref.hpp>
//===============================================
using namespace std;
using namespace boost;
//===============================================
typedef osmium::unsigned_object_id_type idType ;
typedef map<idType, wayData> WayMap;//Define map of Ways ans Their ID
//==================================================
struct VertProperty
{
idType id;
osmium::Location loc;
};
struct EdgeProperty
{
double length;
};
typedef adjacency_list < vecS, vecS, directedS,VertProperty,EdgeProperty > graph_t;
typedef graph_traits < graph_t >::vertex_descriptor Vertex; // Vertex declaration
typedef graph_traits < graph_t >::edge_descriptor Edge; // Edge as link between two Nodes specified by ID
//==================================================
class MyGraphBuilder
{
private: // This Line is Useless just for clarity
graph_t MyGraph;
/////////////////////////////////////////////////////////
public:
MyGraphBuilder(); // default Constructor
MyGraphBuilder(Model); // Parameters Constructor
~MyGraphBuilder(); // Destructor
double Distance(idType,idType); // function to calculate the distance between 2 Verices
//===============================================
//Accessors
// function to get the Graph
graph_t getGraph();
//===============================================
// Mutators
void setGraph(graph_t);
//===============================================
void printGraph() const;
//friend class MyAlgorithm;
};//end of the Class
/////////////////////////////////////
#endif // MYGRAPHBUILDER_H
CPP 文件
// Generic Libraries
//===============================================
#include <math.h>
// Belal Libraries
//===============================================
#include <mygraphbuilder.h>
// Boost Libraries
//===============================================
using namespace std;
using namespace boost;
/////////////////////////////////////////////////
MyGraphBuilder::MyGraphBuilder() // default Constructor
{
Model OurModel;
WayMap MyWayMap = OurModel.getWayMap();
WayMap::iterator it;
// it->first ;// (key = WayID)
// it->second ;// (Value WayData.nodRefList[idType])
for ( it = MyWayMap.begin(); it != MyWayMap.end(); it++ ) // Loop the Whole Way Map
{
unsigned int NodesOfWayIndex = 0; //define Index
idType VertixID; // define Variable to Store Vertix Index
vector<Vertex> WayNodes(it->second.nodeRefList.size());//define a vector of nodes with size of Way
for(auto j = it->second.nodeRefList.begin(); j != it->second.nodeRefList.end(); ++j){// Loop The Whole Nodes of Each way
VertixID = it->second.nodeRefList.at(NodesOfWayIndex);
//VertixID added as Bundeled property name to the vertex
Vertex v = *vertices(MyGraph).first;
Edge e = *edges(MyGraph).first;
//=======================================================
MyGraph[v].id = VertixID;
MyGraph[v].loc = OurModel.getNodeLoaction(VertixID);
//=======================================================
WayNodes[NodesOfWayIndex] = VertixID;
if(NodesOfWayIndex != 0) {
MyGraph[e].length = Distance(WayNodes[NodesOfWayIndex - 1], WayNodes[NodesOfWayIndex]);
}
}
NodesOfWayIndex++;
}
cout<<"\n\n Graph Was Built ...\n\n";
}
//===========================================================================
MyGraphBuilder::MyGraphBuilder(Model OurModel){ // Parameters Constructor
WayMap MyWayMap = OurModel.getWayMap();
WayMap::iterator it;
// it->first ;// (key = WayID)
// it->second ;// (Value WayData.nodRefList[idType])
for ( it = MyWayMap.begin(); it != MyWayMap.end(); it++ ) // Loop the Whole Way Map
{
unsigned int NodesOfWayIndex = 0; //define Index
idType VertixID; // define Variable to Store Vertix Index
vector<Vertex> WayNodes(it->second.nodeRefList.size());//define a vector of nodes with size of Way
for(auto j = it->second.nodeRefList.begin(); j != it->second.nodeRefList.end(); ++j){// Loop The Whole Nodes of Each way
VertixID = it->second.nodeRefList.at(NodesOfWayIndex);
//VertixID added as Bundeled property name to the vertex
Vertex v = *vertices(MyGraph).first;
Edge e = *edges(MyGraph).first;
//=======================================================
MyGraph[v].id = VertixID;
MyGraph[v].loc = OurModel.getNodeLoaction(VertixID);
//=======================================================
WayNodes[NodesOfWayIndex] = VertixID;
if(NodesOfWayIndex != 0) {
MyGraph[e].length = Distance(WayNodes[NodesOfWayIndex - 1], WayNodes[NodesOfWayIndex]);
}
}
NodesOfWayIndex++;
}
cout<<"\n\n Graph Was Built ...\n\n";
}
MyGraphBuilder::~MyGraphBuilder (){ // default Destructor
}
double MyGraphBuilder::Distance(idType Nod1_ID, idType Nod2_ID){ // Function to calculate Euclidean Distance between Vertices
modelData*m_Data = new modelData;
osmium::Location L1,L2; // define any 2 locations on earth
L1 = m_Data->getNodeLoaction(Nod1_ID) ; // get first location
L2 = m_Data->getNodeLoaction(Nod2_ID) ; // get second location
double dist = 0; // distance
double x1 = L1.lat(); // first location latitude
double x2 = L2.lat(); // second location latitude
double y1 = L1.lon(); // first location longitude
double y2 = L2.lon(); // second location longitude
dist = sqrt(pow((x1-x2),2)+pow((y1-y2),2));
return dist;
}
// Accessors
graph_t MyGraphBuilder::getGraph(){
return MyGraph;
}
// Mutators
void MyGraphBuilder::setGraph(graph_t YourGraph){
MyGraphBuilder::MyGraph = YourGraph;
}
//=========================================
void MyGraphBuilder::printGraph()const{
unsigned long long NodesCount = num_vertices(MyGraph);
cout<<"Number of Vertices is :\t"<<NodesCount<<"\n";
unsigned long long EdgesCount = num_edges(MyGraphBuilder::MyGraph);
cout<<"Number of Edges is :\t"<<EdgesCount<<"\n";
//for (auto v : make_iterator_range(vertices(MyGraph))) {
// cout << "Nodes " << v << " name " << MyGraph[v].name << "\n";
// for (auto oe : make_iterator_range(out_edges(v, MyGraph))) {
// cout << "Edge " << oe << " weight " << MyGraph[oe].weight << "\n";
// }
// }
}
【问题讨论】:
-
包含文件的想法是您不必在
#includes 的翻译单元中重复它们的内容。此外,看起来您可能应该只在 cpp 中包含大部分内容,因为它可能只是实现细节 -
-
包含文件
modelDataStructure.h中有什么? -
@Håkon Hægland
#ifndef MODELDATASTRUCTURE_H #define MODELDATASTRUCTURE_H #include <string> #include <vector> #include <osmium/osm.hpp> using namespace std; using tagPair = pair<string,string>; using idType = osmium::unsigned_object_id_type; struct relationMember { osmium::item_type type; idType ref; string role; }; struct relationData { vector<relationMember> memberList; vector<tagPair> tagList; }; struct nodeData { vector<tagPair> tagList; }; struct wayData { vector<idType> nodeRefList; vector<tagPair> tagList; }; #endif // MODELDATASTRUCTURE_H -
@BelalHomaidan 为了让您的问题成为reproducible,您需要在问题中包含所有丢失的文件。也尝试使其成为minimal。
标签: c++ boost graph boost-graph