So I have this function that recursively flows through a tree of vectors of pointers to Locations. Notice the reference parameter int&total
[source lang="cpp"]int score(Location* locationToScore, int&total){ total++; int n = locationToScore->explored?1:0; for (int i=0; i<locationToScore->subLocations.size(); i++) n += score(locationToScore->subLocations[i], total); return n;}[/source]
I call this function like this:
[source lang="cpp"]int total = 0;cout << score(sol, total) << "/" << total;[/source]
But it keeps displaying the total as 0.
Just to make sure I was doing it right I made a simpler program to test it and it works fine in the test program but I can't see a difference:
Test program:
[source lang="java"]#include <iostream>using namespace std;int testFunction(int x, int&y){ y++; if (x==0) return 1; else return 1 + testFunction(x-1, y);}int main(){ int y = 3; cout << testFunction(3, y); cout << y;}[/source]
Show differencesHistory of post edits
#1sooner123
Posted 20 December 2012 - 04:36 AM
So I have this function that recursively flows through a tree of vectors of pointers to Locations. Notice the reference parameter int&total
[source lang="cpp"]int score(Location* locationToScore, int&total){ total++; cout << total; int n = locationToScore->explored?1:0; for (int i=0; i<locationToScore->subLocations.size(); i++) n += score(locationToScore->subLocations[i], total); return n;}[/source]
I call this function like this:
[source lang="cpp"]int total = 0;cout << score(sol, total) << "/" << total;[/source]
But it keeps displaying the total as 0.
Just to make sure I was doing it right I made a simpler program to test it and it works fine in the test program but I can't see a difference:
Test program:
[source lang="java"]#include <iostream>using namespace std;int testFunction(int x, int&y){ y++; if (x==0) return 1; else return 1 + testFunction(x-1, y);}int main(){ int y = 3; cout << testFunction(3, y); cout << y;}[/source]
[source lang="cpp"]int score(Location* locationToScore, int&total){ total++; cout << total; int n = locationToScore->explored?1:0; for (int i=0; i<locationToScore->subLocations.size(); i++) n += score(locationToScore->subLocations[i], total); return n;}[/source]
I call this function like this:
[source lang="cpp"]int total = 0;cout << score(sol, total) << "/" << total;[/source]
But it keeps displaying the total as 0.
Just to make sure I was doing it right I made a simpler program to test it and it works fine in the test program but I can't see a difference:
Test program:
[source lang="java"]#include <iostream>using namespace std;int testFunction(int x, int&y){ y++; if (x==0) return 1; else return 1 + testFunction(x-1, y);}int main(){ int y = 3; cout << testFunction(3, y); cout << y;}[/source]