写了个汉诺塔,使用全局变量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. php实现对数组进行编码转换

    1.转换GB2312编码为UTF-8 //更改编码为utf8 protected function array2utf8($array){ $array = array_map(function($v ...

  2. CXF - JAX-WS入门

    相关dependency,我使用的版本是2.7.11: <dependency> <groupId>org.apache.cxf</groupId> <art ...

  3. 使用connect-multiparty限制nodejs图片上传

    connect-multiparty中间件,可用于获取文件上传时各种参数,比如文件大小.格式等,具体使用: var multipart = require('connect-multiparty'); ...

  4. ubuntu下部署django

    一:保证python运行环境安装 apt-get install python 如果需要进行开发则还要安装python-dev apt-get install python-dev 二:安装djang ...

  5. [android] 界面切换的核心方法

    根据效果图拆分界面 主体部分 View ==> ViewGroup ==> RelativeLayout,主体部分使用RelativeLayout作为占位 View和ViewGroup的区 ...

  6. 一:SpringMVC架构流程

    架构流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. 3.处理器映射器根据请求url ...

  7. openJDK的下载

    http://jdk7.java.net/source.html # 失效 http://download.java.net/openjdk/jdk7 # 无效,点击文件下载跳转错误页面 http:/ ...

  8. getElementsByName属性

    getElementsByName() 方法可返回带有指定名称的对象的集合 语法 document.getElementsByName(name) 该方法与 getElementById() 方法相似 ...

  9. Linux下查看Tomcat的控制台输出信息

    Linux下查看Tomcat的控制台输出信息 首先使用SSH连接到数据库,然后点击window创建一个new terminal, 进入tomcat/logs/文件夹下,输出控制台信息,命令如下: cd ...

  10. Grid标签计算结果集中的合计行

    方法一: var detailT= grid.gettable("11"); var r=detailT.rows.length; detailT.rows[r-1].cells[ ...