You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++递归汉诺塔程序编译报错:operator<<不匹配及Error C2679问题求助

解决汉诺塔代码中的编译错误(Error C2679)

嘿,我来帮你搞定这个编译错误!你遇到的no operator << matches these operands和Error C2679,根源其实是变量作用域的问题,具体来说:

错误原因

你在toh函数里定义了static int count = 0;,这个static变量的作用域被严格限制在toh函数内部——也就是说,main函数根本看不到这个count变量!

编译器找不到你在main里写的count,就会去标准库里找有没有同名的东西,恰好标准库中有一个std::count函数(用来统计容器中元素的数量),所以它误以为你要把这个重载函数传给cout <<,这就触发了那个“不匹配运算符”的错误。

修复方案

这里给你两种简单可行的修复方式,适合初学者:

方案1:把count改成全局变量(最简单)

把static int count = 0;从toh函数里移出来,放到所有函数的外面(比如using namespace std;之后),这样main和toh都能访问到它:

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

// 把count改成全局变量
static int count = 0;

void toh(int p, char from, char to, char a){
    if (p == 0) {
        return;
    } 
    if (p == 1) {
        cout << "from " << from << " to " << to << endl;
        count++;
        return;
    } 
    toh(p - 1, from, a, to);
    cout << "from " << from << " to " << to << endl;
    count++;
    toh(p - 1, a, to, from);
} 

int main() {
    int num;
    bool t = 1;
    do {
        // 每次循环重置count,避免多次运行累加
        count = 0;
        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 << "\nContinue? (1=yes 0=no) : ";
        cin >> t;
    } while (t);
    system("pause");
    return 0;
} 

注意:我还加了count = 0;在do-while循环开头,这样每次重新输入圆盘数量时,计数会重置,不会和上一次的结果累加。

方案2:用引用传递count(更规范,避免全局变量)

如果你不想用全局变量,可以把count作为引用参数传给toh函数,这样递归过程中能直接修改main里的变量:

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

// 修改toh函数,添加引用参数count
void toh(int p, char from, char to, char a, int& count){
    if (p == 0) {
        return;
    } 
    if (p == 1) {
        cout << "from " << from << " to " << to << endl;
        count++;
        return;
    } 
    toh(p - 1, from, a, to, count);
    cout << "from " << from << " to " << to << endl;
    count++;
    toh(p - 1, a, to, from, count);
} 

int main() {
    int num;
    bool t = 1;
    do {
        int count = 0; // 在main里定义count
        cout << "Enter the No. of the disks : ";
        cin >> num;
        cout << "source 1 target 2 temporary 3" << endl;
        // 调用时传入count的引用
        toh(num, '1', '2', '3', count);
        cout << "2 to the " << num << " power = " << pow(2, num) << endl;
        cout << "Number of moves " << count;
        cout << "\nContinue? (1=yes 0=no) : ";
        cin >> t;
    } while (t);
    system("pause");
    return 0;
} 

额外小提示

你用pow(2, num)计算2的n次方时,因为pow返回的是double类型,输出可能会显示成8.0这样的小数。如果想要整数结果,可以用位运算(1 << num)(当num不超过你的int类型位数时,比如31位),这样更准确也更高效。

内容的提问来源于stack exchange,提问作者user11495912

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:42:46