1

I'm new to c++ and working on a recursive towers of hanoi project. I have it completely done except for one small error. The second << in "cout << "Number of moves " << count; is giving me the error "no operator "<<" matches these operands". I understand it has something to do with count, but I am uncertain on how to fix it.

EDIT: There is also this error for the same line Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'overloaded-function'

CODE:

#pragma warning(disable: 4996) 
#include<string> 
#include<stdlib.h> 
#include<time.h>
#include<iostream>
#include<cmath>

using namespace std;

void toh(int p, char from, char to, char a){
    static int count = 0;
    if (p == 0) {
        return;
    } //if end

    if (p == 1) {
        cout << "from " << from << " to " << to << endl;
        count++;
        return;
    } //if end

    toh(p - 1, from, a, to);
    cout << "from " << from << " to " << to << endl;
    count++;
    toh(p - 1, a, to, from);
} //toh end

int main() {
    int num; bool t = 1;
    do {
        cout << "Enter the No. of the disks : ";
        cin >> num;
        cout << "source 1 target 2 temporary 3" << endl;
        toh(num, '1', '2', '3');
        cout << "2 to the " << num << " power = " << pow(2, num) << endl;
        cout << "Number of moves " << count;
        cout << "Continue? (1=yes 0=no) : ";
        cin >> t;
    } while (t);

    system("pause");
    return 0;
} //main end
  • Prefer to use `left shift` when performing "2 to the power of`. The `pow` function is floating point and may introduce inaccuracies when converting to/from integers. A faster method is `(1 << num)`, which is likely one instruction on most processors. – Thomas Matthews Oct 29 '21 at 22:42
  • 1
    `count` is not declared in `main`; you could move the `static int count = 0` out of `toh`. – rturrado Oct 29 '21 at 22:43
  • FYI, get into the habit of using `true` and `false` with a `bool` type. The 1 & 0 technique is like so ancient (like 1960's). – Thomas Matthews Oct 29 '21 at 22:43
  • 1
    A better solution than using a static would probably be to have `toh` return the number of moves (e.g. something like https://godbolt.org/z/ExWzEazhb, I didn't check it very much). – rturrado Oct 29 '21 at 22:45
  • @rturrado Really helpful, though it doesn't give the correct number of moves – user11495912 Oct 29 '21 at 23:11
  • Note that the confusing error is due to `using namespace std;`. You're attempting to print the function [`std::count`](https://en.cppreference.com/w/cpp/algorithm/count) (not the return value, but the function itself), which obviously isn't what you want. Yet another reason why [`using namespace std;` is bad](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Miles Budnek Oct 29 '21 at 23:47
  • @user11495912 Yes, sorry about that, I didn't test it at all. This should give you the expected result: https://godbolt.org/z/583rbMfKP – rturrado Oct 31 '21 at 18:15

1 Answers1

1

The variable count is defined local to the function toh, so you can return the count value which can be assigned to a variable called count inside main, allowing you to use it in the line cout << "Number of moves " << count;. It should look like this:

#include <stdlib.h> 
#include <time.h>
#include <iostream>
#include <cmath>

using namespace std;

static int toh(int p, char from, char to, char a){
    static int count = 0;
    if (p == 0) {
        return count;
    } //if end

    if (p == 1) {
        cout << "from " << from << " to " << to << endl;
        count++;
        return count;
    } //if end

    toh(p - 1, from, a, to);
    cout << "from " << from << " to " << to << endl;
    count++;
    toh(p - 1, a, to, from);
    return count;
} //toh end

int main() {
    int num, count; bool t = 1;
    do {
        cout << "Enter the No. of the disks : ";
        cin >> num;
        cout << "source 1 target 2 temporary 3" << endl;
        count = toh(num, '1', '2', '3');
        cout << "2 to the " << num << " power = " << pow(2, num) << endl;
        cout << "Number of moves " << count;
        cout << "Continue? (1=yes 0=no) : ";
        cin >> t;
    } while (t);

    system("pause");
    return 0;
} //main end
Daniel
  • 275
  • 1
  • 9
  • 1
    Thank you! This error was giving me a huge headache. I did have to make a small adjustment in erasing the $ after num and putting ); after because it was giving me an error but it works! – user11495912 Oct 29 '21 at 23:31
  • @user11495912 Yes that is not meant to be there, I copied it from my text editor and that was where the text went off screen (I edited the code in the terminal). I will edit it so it is correct, thank you. – Daniel Oct 29 '21 at 23:34
  • Don't you need a `return count;` at the end of `toh`? – rturrado Oct 29 '21 at 23:59
  • @rturrado The function never reaches the end because it recursively calls itself until it reaches a `return count;` from one of the `if` statements, so a `return count` at the end would never get called. – Daniel Oct 30 '21 at 09:55
  • 1
    @Daniel What happens after you call `toh` from the last line in `toh`? Where do you return to? – rturrado Oct 31 '21 at 00:33
  • @rturrado Ah yes, I see now, even though it works without a return at the end, there probably should be. I'll edit it in. – Daniel Oct 31 '21 at 10:03
  • @Daniel If you call `toh` with `p>1` you reach the end of the function without returning anything. See the compiler warning: https://godbolt.org/z/sqKKbcjo7. It's true that it works (because you reach the end of the function right after returning from the `toh` call and you return what you have in `eax` register, that happens to be precisely what you want) but that's undefined behaviour. You definitely need that `return count;`. – rturrado Oct 31 '21 at 18:13
  • 1
    @rturrado Yes you are right, I have edited my answer with it in, I did not think about that when I wrote the answer. Thank you for pointing it out. – Daniel Oct 31 '21 at 19:00