写了个汉诺塔,使用全局变量count来记录步数,结果Error:count不明确

#include <iostream>
using namespace std;
int count = ; void hanoi(int num, char source, char through, char target){
if (num == ) return;
hanoi(num - , source, target, through);
printf("%d from %c to %c\n", num, source, target);
count++;
hanoi(num - , through, source, target);
}

后来才知道 std命名空间里有std::count,所以与全局变量count冲突

std::count

template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
​ count(InputIterator first, InputIterator last, const T& val);

所以修改方法有以下几种:

1.全局变量count改为cnt(或其他名称)

2.使用count的地方改为::count

#include <iostream>
using namespace std;
int count = ; void hanoi(int num, char source, char through, char target){
if (num == ) return;
hanoi(num - , source, target, through);
printf("%d from %c to %c\n", num, source, target);
::count++;
hanoi(num - , through, source, target);
}

3.不要使用using namespace std,使用using namespace std是不好的习惯

C++ 全局变量不明确与 using namespace std 冲突的更多相关文章

  1. Error:全局变量不明白(using namespace std 与全局变量的冲突)

    在用递归写八皇后时,定义了一个全局变量count,结果出现故障例如以下:提示全局变量不明白. 最后发如今实现文件.cpp中.我使用了using  namespace std; 解决方法: 1.使用co ...

  2. #include<iostream.h>与#include<iostream> using namespace std的区别

    所谓namespace,是指标识符的各种可见范围.C++标准程序库中的所有标识符都被定义于一个名为std的namespace中.  一 :<iostream>和<iostream.h ...

  3. 关于C++中using namespace std

    原文链接:http://www.kuqin.com/language/20080107/3532.html <iostream>和<iostream.h>是不一样,前者没有后缀 ...

  4. (C++)浅谈using namespace std

    1.<iostream>和<iostream.h> 在你的编译器include文件夹里面可以看到,二者是两个文件,里面的代码是不一样的. 后缀为.h的头文件c++标准已经明确提 ...

  5. using namespace std 是什么意思?

    摘录CSDN上面大牛的回答简要意思就是使用标准库,想知道更清楚的继续读下面的. using   namespace   std   意思:   using   和namespace都是C++的关键词. ...

  6. [转载]C++之using namespace std 详解与命名空间的使用

    来源:https://blog.csdn.net/Bruce_0712/article/details/72824668 所谓namespace,是指标识符的各种可见范围.C++标准程序库中的所有标识 ...

  7. using namespace std 和 using std::cin

    相较using std::cin使用using namespace std不会使得程序的效率变低,或者稳定性降低,只是这样作会将很多的名字引入程序,使得程序员使用的名字集合变小,容易引起命名冲突. 在 ...

  8. namespace std

    c++中使用namespace来防止命名冲突(重命名),我们经常使用的一些函数和变量都被放在一个叫std的namespace中,如标准I/O流操作,vector等等.我们在每一个文件中都可使用std中 ...

  9. C++ using namespace std(转载)

    转载自http://www.kuqin.com/language/20080107/3532.html 感谢这位大神的解答! 以下的内容摘抄自转载的文章里面的部分内容. 早些的实现将标准库功能定义在全 ...

随机推荐

  1. Firebird with lock

    Firebird 锁,默认是行级锁,即记录锁. 通常最常用的是 with lock ,即:将查出的所有记录都锁定,但允许其他事务读取,不允许其他事务更新.删除.本事务允许更新. 另一种 for upd ...

  2. MySQL 字段全部转换成小写

    原因: 因为框架某些字段大写有时候不被正确识别,所以字段都修改成小写; 特别说明:因为这里只有表,没有视图,存储过程等等其它所以我可以直接这么写; 步骤: 1.导出结构语句 2. 执行C# 脚本,替换 ...

  3. linq——常用方法

    take  前几 skip   跳过前几 takeWhile   var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);  / ...

  4. Vue-[v-model]理解示例

    对应文档节点: https://vuefe.cn/v2/guide/components.html#Customizing-Component-v-model <body> <div ...

  5. Springboot简单整合Rabbit

    两个项目.分别是生产者和消费者项目 .首先引入依赖.两边pom都一样 第一次练习,启动生产者后,再启动消费者,一直报找不到 队列的声明. 后排查发现是  需要现在生产者这边浏览器访问一次生产消息的方法 ...

  6. OpenStack系列

    一.概述 云计算介绍 OpenStack各组件详解和通信流程 二.keystone系列 三.glance系列 四.nova系列 虚拟化介绍 kvm介绍 五.neutron系列 六.horizon系列 ...

  7. 记录开发Nodejs c++ addon的一些经验(二、数据类型的转换(尤其是Buffer))

    常见的数据类型的转换基本比较容易,结合nan应该不是一件难事 参考链接: http://blog.jobbole.com/109598/ http://deadhorse.me/nodejs/2012 ...

  8. 小白学习css记录

    一.复习 什么是CSS? 层叠样式表 -层叠样式只会被覆盖而不会被替代 CSS的使用方式 style属性---> <h1 style="css属性"></h ...

  9. 机智的Popup,带着简单的图表感觉酷酷的

    之前有提过用 InfoTemplate 类对 FeatureLayer 的基本信息进行展示,今天为大家介绍 esri/dijit/Popup 的使用,这东西还有 简单的图表展示功能呢! <!DO ...

  10. Codeforces Round #413 B. T-shirt buying

    B. T-shirt buying time limit per test   3 seconds memory limit per test   256 megabytes   A new pack ...