do you see anything wrong with my code: #include "itinerary.h" #include "graph.h"…
- User
do you see anything wrong with my code: #include "itinerary.h" #include "graph.h" #include "graphUtils.h"
#include
using namespace std; using namespace boost;
Itinerary::Itinerary() : tripLength(0) { }
void Itinerary::addRoad(const Leg& roadSegment) { Vertex v1, v2;
if (verticesByName.find(roadSegment.fromCity) == verticesByName.end()) { v1 = boost::add_vertex(g); verticesByName[roadSegment.fromCity] = v1; } else { v1 = verticesByName[roadSegment.fromCity]; } if (verticesByName.find(roadSegment.toCity) == verticesByName.end()) { v2 = boost::add_vertex(g); verticesByName[roadSegment.toCity] = v2; } else { v2 = verticesByName[roadSegment.toCity]; } boost::add_edge(v1, v2, g); boost::add_edge(v2, v1, g);}
std::vector Itinerary::travelPlan(std::string startingCityName, std::string destinationCityName) { Vertex start = verticesByName[startingCityName]; Vertex end = verticesByName[destinationCityName];
// Assume weightMap is a map of Edge to double that represents the distances between vertices std::map<Edge, double> weightMap; for (auto edge_it = boost::edges(g).first; edge_it != boost::edges(g).second; ++edge_it) { Edge edge = *edge_it; weightMap[edge] = g[edge].length; } std::vector<Vertex> path = findWeightedShortestPath(g, start, end, weightMap); std::vector<Leg> results; if (!path.empty()) { for (int i = 0; i < path.size() - 1; ++i) { Leg leg; leg.fromCity = g[path[i]].city; leg.toCity = g[path[i + 1]].city; leg.distance = weightMap[boost::edge(path[i], path[i + 1], g).first]; results.push_back(leg); } } return results;}
double Itinerary::getLastTripLength() const { return tripLength; }
Assistant
There are a few issues in your code. Here are the main points:
-
In the
addRoadfunction, you are usingverticesByNameto store and retrieve vertices. However, you have not definedverticesByNamein theItineraryclass. You should define it as a member variable of theItineraryclass. -
In the
travelPlanfunction, you are usingweightMapto store the weights of the edges. However, you have not definedweightMapin theItineraryclass. You should define it as a member variable of theItineraryclass or pass it as an argument to the function.
Here's the corrected code:
#include "itinerary.h" #include "graph.h" #include "graphUtils.h" #include <iostream> using namespace std; using namespace boost; Itinerary::Itinerary() : tripLength(0), verticesByName(make_assoc_property_map(StringToVertexMap)) { } void Itinerary::addRoad(const Leg& roadSegment) { Vertex v1, v2; if (verticesByName.find(roadSegment.fromCity) == verticesByName.end()) { v1 = boost::add_vertex(g); verticesByName[roadSegment.fromCity] = v1; } else { v1 = verticesByName[roadSegment.fromCity]; } if (verticesByName.find(roadSegment.toCity) == verticesByName.end()) { v2 = boost::add_vertex(g); verticesByName[roadSegment.toCity] = v2; } else { v2 = verticesByName[roadSegment.toCity]; } boost::add_edge(v1, v2, g); boost::add_edge(v2, v1, g); } std::vector<Leg> Itinerary::travelPlan(std::string startingCityName, std-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.