I am trying to print the path through a graph made by my DFS algorithm. I am having errors which I do not understand.
void GraphTraversal::printPath(std::vector<const Node *> &path)
{
string myPath;
for(int i = 0; i<path.size(); i )
{
string dfspath = to_string(dfspath[i].getNodeID()); // i get an error here - expression must have class type but it has type "char"
std::cout<<dfspath;
}
paths.insert(myPath);
}
I have coded my DFS algorithm as such.
void GraphTraversal::DFS(set<const Node *> &visited, vector<const Node *> &path, const Node *src, const Node *dst)
{
visited.insert(src);
path.push_back(src);
if (src == dst)
{
printPath();
}
for(const auto &e: src->getOutEdges())
{
if(!visited.count(e->getDst())){
DFS( visited, path, e->getDst(), dst);
}
}
visited.erase(src);
path.pop_back();
}
The output I would like from my printPath method is for string singlePath to output "START: 1->2->4->5->END" with the digits being the nodeID.
CodePudding user response:
You declare a string
variable named path
, but your parameter is already named path
.
So path[i]
actually refers to the string
, not to the vector
.
Also, path[i]
is a const Node*
, so you must call it's function with : path[i]->getNodeID()
.