写了个汉诺塔,使用全局变量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. 常用工具说明--Git和GitHub简明教程

    一.Git的主要功能:版本控制 版本:想想你平时用的软件,在软件升级之后,你用的就是新版本的软件.你应该见过这样的版本号:v2.0 或者 1511(表示发布时为15年11月),如下图:那么如果你修改并 ...

  2. js惊奇效果分享,和排序算法

    分享地址:http://www.cnblogs.com/lhb25/p/8-amazing-codepen-demos.html 排序算法分享地址:http://www.w3cplus.com/js/ ...

  3. ios audio不能自动播放

    今天做了一个简单的落地页项目,就是类似于手机微信上经常看到的滑动效果.因为公司要求需要自己开发,所以我就用swiper+swiper.animate开发,开发速度很快,只不过最后音乐哪里出现了一点小b ...

  4. js打印去掉页眉页脚

    <style type="text/css" media="print"> @page /* 实现代码 */ { size: auto; /* au ...

  5. 八、profile多环境配置

    通常我们的程序有着多个环境: 1.开发环境: 2.生产环境. 等 环境的配置各不相同,我们希望通过一个简单的配置来切换环境,而springboot轻松地实现了该功能: 一.多环境需要多配置文件 一般我 ...

  6. request对象域和转发

    1.request是一个域对象,具备以下方法 setAttribute(string name,Object O) getAttribute(String name) removeAttribute( ...

  7. css3+javascript旋转的3d盒子

    今天写点css3,3d属性写的3d盒子,结合javascript让盒子随鼠标旋转起来 今天带了css3新属性3d <!DOCTYPE html> <html> <head ...

  8. vue2.0中的watch和计算属性computed

    watch和computed均可以监控程序员想要监控的对象,当这些对象发生改变之后,可以触发回调函数做一些逻辑处理 watch监控自身属性变化 <!DOCTYPE html> <ht ...

  9. Java基础之Java中的泛型

    1.为什么要使用泛型 这里我们俩看一段代码; List list = new ArrayList(); list.add("CSDN_SEU_Cavin"); list.add(1 ...

  10. python numpy+mkl+scipy win64 安装

    用pip在windows下安装numpy,scipy等库时一般来说都不会很顺利比较好的方式是自己下载对应的whl文件pip install 话不多说上链接 http://www.lfd.uci.edu ...