Hanoi

Not too long ago I saw a post about recursive functions, one of them had a rather odd name: SolveHanoi. Naturally, I googled the subject and found a neat mathemathical puzzle. Unwilling to build a little wooden puzzle myself, I decided to simulate it in a console app (this is just some small program that I don't intend to release, and since outputting of stl containers is already defined, there are no hackish displays tricks going on, so don't complain about the console.) I thought I would make it so, that every "staff" is a vector of unsigned's, in which the unsigned integer represents the bigness of the ring at that place. To make it work in a LIFO context, I used a vector with push_back and pop_back, because it enables me to have random access to all elements, which makes it easier to create a drawing code. I put all vectors of unsigneds into a new unsigned, which represents the game board on which rings can be exchanged. Unfortunately, I didn't get it to work. Here is my code:
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
#include <iostream>
#include <vector>

using namespace std;

template<class T>
bool Move(vector<T> x, vector<T> y)
{
   if (!x.empty() && (x[x.size()-1]<y[y.size()-1] || y.empty()))
   {
      y.push_back(x[x.size()-1]);
      x.pop_back();
      return true;
   }
   return false;
}

template<class T>
void print(ostream& os, const vector<T>& v)
{
   for(unsigned i = 0;i<v.size();i++) os << v[i];
}

int main()
{
   vector< vector<unsigned>& > Game;
   vector< unsigned > First;
   vector< unsigned > Second;
   vector< unsigned > Third;
   Game.push_back(First);
   Game.push_back(Second);
   Game.push_back(Third);
   for(unsigned u = 5; u>=1; u--) First.push_back(u);
   print<unsigned>(cout, First);
}
Line 26 is definitely wrong.

Actually, lines 26 through 32 are nothing more than

 
std::vector< std::vector< unsigned > > Game( 3, std::vector< unsigned >() );


Thanks, changed it, it works now! I just could not sort this error out because I was using Dev-Cpp (I was at school at the time, I had to have a lightweight IDE with me, I put this one on my USB drive).
Topic archived. No new replies allowed.