function not declear in main function
hi can anybody help me figure out why the error shows that two functions(get_graph and findshortpath) are not declare in scope.
i got three files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <vector>
#include "Assignment3.h"
using namespace std;
void Map:: reset_nodes(Map &nm){
map<string,Node*>::iterator im;
for(im = nm.nodemap.begin(); im != nm.nodemap.end(); im++){
(*im).second->cost = 0;
(*im).second->back_pointer = 0;
(*im).second->visited = false;
}
}
void Map:: findShortpath(string s,string t,Map &nodes){
// check and report or abort
Node* source = nodes.nodemap[s];
if(source==0){cout << s << " not in map " << endl; return;}
else cout << s << " in map " << endl;
Node* target = nodes.nodemap[t];
if(target==0){cout << t << " not in map " << endl; return;}
else cout << t << " in map " << endl;
reset_nodes(nodes);
// put the source into pq and loop until empty
priority_queue<Node *, deque<Node*>, compare>pq;
pq.push(source);
while(!pq.empty()){
// process least cost node.
Node* curr = pq.top();
pq.pop();
curr->visited = true;
// process neighbors
list<Edge*>::iterator edge;
for(edge = curr->neighbors.begin(); edge != curr->neighbors.end(); edge++){
Node *ne = (*edge)->dest;
if(!ne->visited){
ne->cost += (*edge)->weight + curr->cost;
ne->visited = true;
ne->back_pointer = curr;
cout << " pushing " << ne->name << " cost " << ne->cost << endl;;
pq.push(ne);
}
}
}
}
void Map:: get_graph(string const &filename, Map &node_map)
{
ifstream inf(filename.c_str());
string from, to;
double weight;
while ( inf.good() )
{
inf >> from >> to >> weight;
if ( inf.good() )
{
Node *Target = node_map.find_in_nodemap(to);
Node *Source = node_map.find_in_nodemap(from);
Edge *connector = new Edge(weight,Target);
Source->neighbors.push_back(connector);
}
}
}
|
file2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <vector>
#include "Assignment3.h"
using namespace std;
int main(){
Map nodes;
get_graph("graph.txt", nodes);
cout << "after read graph" << endl;
cout << nodes;
string s,t;
do{
cout << " Please enter a source" << endl;
cin >> s;
cout << " Please enter a target" << endl;
cin >> t;
findShortpath(s,t,nodes);
cout << "Enter y to continue " << endl;
cin >> s;
}while(s == "y");
}
|
file3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <vector>
using namespace std;
class Edge;
class Node {
public:
string name;
list<Edge*> neighbors;
bool visited;
double cost;
Node *back_pointer;
Node(string const &n): name(n),visited(false),cost(0){}
};
class Edge
{
public:
double weight;
Node * dest;
Edge(double c,Node *d = NULL):weight(c),dest(d){}
~Edge(){if(dest)delete dest;}
};
class Map{
public:
map<string,Node*> nodemap;
void findShortpath(string s,string t,Map &nodes);
void get_graph(string const &filename, Map &node_map);
Node* find_in_nodemap(string const &name){
Node* result = nodemap[name];
if ( result == 0 )
result = nodemap[name] = new Node(name);
return result;
}
friend ostream& operator<<(ostream& o, Map nm){
map<string,Node*>::iterator im;
for(im = nm.nodemap.begin(); im != nm.nodemap.end(); im++){
o << (*im).second->name << endl;
list<Edge*> neighbors = (*im).second->neighbors;
list<Edge*>::iterator e;
for(e = neighbors.begin(); e != neighbors.end(); e++){
cout << " -> " << (*e)->dest->name << " weight " << (*e)->weight <<endl;
}
}
return o;
}
};
struct compare{
bool operator()(Node * &a,Node * &b)const{
return b->cost < a->cost;
}
};
|
thanks for helping me
get_graph is a member function of your map object.
nodes.get_graph("graph.txt", nodes);
Last edited on
Topic archived. No new replies allowed.