Bit rusty with C++ and not sure how to fix these errors i'm getting. Any help is much appreciated!!!
Here's the errors:
DFS.cpp: In function 'void DFS(Node*)':
DFS.cpp:64: error: cannot convert 'Node' to 'Node*' for argument '1' to 'void DFS_Visit(Node*)'
DFS.cpp: In function 'Node* makeGraph()':
DFS.cpp:130: error: no match for 'operator*' in '**(((Node*)(((long unsigned int)i) * 16ul)) + graph)'
DFS.cpp: In function 'Node* makeSample()':
DFS.cpp:150: error: no match for 'operator=' in '*(((Node*)(((long unsigned int)i) * 16ul)) + graph) = 0l'
DFS.cpp:15: note: candidates are: Node& Node:: operator=(const Node&)
DFS.cpp:158: error: name lookup of 'i' changed for new ISO 'for' scoping
DFS.cpp:148: error: using obsolete binding at 'i'
And here's the code:
Code:#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int operationsCounter=0; int ntime=0, verts, edges; string* color = new string[1]; int* pi = new int[1]; int* d = new int[1]; int* f = new int[1]; struct Node{ int data; Node* next; }; void InsertAfter(Node *head, int value){ if(head==NULL){ Node* temp=NULL; temp = new Node; temp->data = value; temp->next = NULL; head = temp; }else{ Node *temp = new Node; temp->data = value; temp->next = (head)->next; (head)->next = temp; } } void DFS_Visit(Node* u) { color[u->data]="gray"; d[u->data]=(++ntime); int v=u->data; while((u=u->next)!=NULL) { if (color[u->data]=="white") { pi[u->data]=v; DFS_Visit(u); } } color[v]="black"; f[v]=(++ntime); } void DFS(Node* G) { for (int u=0; u<verts; u++) { color[u]="white"; pi[u]=-1; } ntime=0; for (int u=0; u<verts; u++) { if (color[u]=="white") { DFS_Visit(G[u]); } } } void printResults() { cout << " d f pi " << endl; for (int i=0; i<verts; i++) { if(i<10) cout << " " << i; else if(i<100) cout << " " << i; else if(i<1000) cout << " " << i; else cout << " " << i; if(d[i]<10) cout << " " << d[i]; else if(d[i]<100) cout << " " << d[i]; else if(d[i]<1000) cout << " " << d[i]; else cout << " " << d[i]; if(f[i]<10) cout << " " << f[i]; else if(f[i]<100) cout << " " << f[i]; else if(f[i]<1000) cout << " " << f[i]; else cout << " " << f[i]; if(pi[i]<10) cout << " " << pi[i] << endl; else if(pi[i]<100) cout << " " << pi[i] << endl; else if(pi[i]<1000) cout << " " << pi[i] << endl; else cout << " " << pi[i] << endl; } cout << endl << endl; cout << "Number of operation: " << operationsCounter << endl; } bool alreadyInList(Node* llist, int data) { if(llist==NULL) return false; else if(data==llist->data) return true; else return alreadyInList(llist->next, data); } Node* makeGraph() { int u, v; u=0; v=0; Node* graph = new Node[5]; for(int i=0; i<5; i++) { &graph[i]=NULL; InsertAfter(&graph[i], i); } for(int i=0; i<edges; i++) { while(alreadyInList(&graph[u], v)) { u = rand() % verts; v = rand() % verts; } InsertAfter(&graph[u], v); } return graph; } Node* makeSample() { Node* graph = new Node[5]; for(int i=0; i<verts; i++) { graph[i]=NULL; InsertAfter(&graph[i], i); } InsertAfter(&graph[0], 1); InsertAfter(&graph[1], 2); InsertAfter(&graph[1], 3); InsertAfter(&graph[2], 3); InsertAfter(&graph[2], 4); InsertAfter(&graph[4], i); return graph; } int main() { int option; while (option!=3) { cout << "Please select an option: " << endl; cout << "1) Run DFS on the sample graph" << endl; cout << "2) Run DFS on a customized random graph" << endl; cout << "3) Exit" << endl; cin >> option; if(option==1) { verts=5; edges=6; color = new string[verts]; pi = new int[verts]; d = new int[verts]; f = new int[verts]; operationsCounter=0; DFS(makeSample()); printResults(); } if(option==2) { cout << "How many vertices would you like the graph to have?" << endl; cin >> verts; cout << "How many edges would you like the graph to have?" << endl; cin >> edges; color = new string[verts]; pi = new int[verts]; d = new int[verts]; f = new int[verts]; operationsCounter=0; DFS(makeGraph()); printResults(); } } cout << "Thank you. Goodbye!"; return 0; }



Pass them to the functions as arguments. There is yet another way, which I think manav is hinting at: they may be better off as member variables of a class.