写了个汉诺塔,使用全局变量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. vuex 实现vue中多个组件之间数据同步以及数据共享。

    http://pan.baidu.com/s/1hrJfpli  demo下载地址 前言 在一些项目中有很多数据状态之间要实现数据共享状态共享,例如购物车的数据.用户的登录状态等等.vue父元素是可以 ...

  2. webpack使用extract-text-webpack-plugin打包时提示错误Use Chunks.groupsIterable and filter by instanceof Entryp

    转自:https://blog.csdn.net/gezilan/article/details/80020417 前提条件: 当前时间是2018年4月20日. webpack的最新版本为是 v4.6 ...

  3. office2007安装时显示安装程序找不到 office.zh-cn\officeLR.cab怎么办

    根本原因是和VS2008有关解决方法如下:1. 找到vs2008安装程序(光盘,镜像文件,解压文件都一样),找到WCU文件夹在他里面找到WebDesignerCore文件夹,然后打开它找到WebDes ...

  4. Gradle sync failed: Cause: org.gradle.logging.StyledTextOutput$Style Consult IDE log for more details

    环境 Android studio 3.0 导入开源中国: ... dependencies { //noinspection GradleDependency classpath 'com.andr ...

  5. Heka 的配置文件加载逻辑

    Heka 使用的是 TOML 格式的配置文件, 有关 golang 加载 TOML 配置文件的技术请参看:  http://www.cnblogs.com/ghj1976/p/4082323.html ...

  6. Linux From Scratch(从零开始构建Linux系统,简称LFS)(三)

    九. 系统配置 1. 安装 LFS-Bootscripts-20150222 软件包包含一套在 LFS 系统启动和关闭时的启动和停止脚本. cd /sources tar -jxf lfs-boots ...

  7. k8s安装部署过程个人总结及参考文章

    以下是本人安装k8s过程 一.单机配置 1. 环境准备 主机名 IP 配置 master1 192.168.1.181 1C 4G 关闭所有节点的seliux以及firewalld sed -i 's ...

  8. spring和jdbc结合的一个小例子

    1.新建一个SpringJdbc的maven项目. 2.引入需要的jar包的依赖 <span style="white-space:pre"> </span> ...

  9. Chrome 声音自动播放抱错问题【play() failed】

    Chrome下调用play后抱错:DOMException: play() failed because the user didn't interact with the document firs ...

  10. Mybatis学习随笔

    学习Mybatis路径(适合有java基础和mysql基础的小伙伴) 1.把项目搭建起来,跑一跑感受一下 2.测试基本映射 3.测试高级映射 4.测试动态sql 5.学习懒加载与缓存 6.与sprin ...