C++ 全局变量不明确与 using namespace std 冲突
写了个汉诺塔,使用全局变量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 冲突的更多相关文章
- Error:全局变量不明白(using namespace std 与全局变量的冲突)
在用递归写八皇后时,定义了一个全局变量count,结果出现故障例如以下:提示全局变量不明白. 最后发如今实现文件.cpp中.我使用了using namespace std; 解决方法: 1.使用co ...
- #include<iostream.h>与#include<iostream> using namespace std的区别
所谓namespace,是指标识符的各种可见范围.C++标准程序库中的所有标识符都被定义于一个名为std的namespace中. 一 :<iostream>和<iostream.h ...
- 关于C++中using namespace std
原文链接:http://www.kuqin.com/language/20080107/3532.html <iostream>和<iostream.h>是不一样,前者没有后缀 ...
- (C++)浅谈using namespace std
1.<iostream>和<iostream.h> 在你的编译器include文件夹里面可以看到,二者是两个文件,里面的代码是不一样的. 后缀为.h的头文件c++标准已经明确提 ...
- using namespace std 是什么意思?
摘录CSDN上面大牛的回答简要意思就是使用标准库,想知道更清楚的继续读下面的. using namespace std 意思: using 和namespace都是C++的关键词. ...
- [转载]C++之using namespace std 详解与命名空间的使用
来源:https://blog.csdn.net/Bruce_0712/article/details/72824668 所谓namespace,是指标识符的各种可见范围.C++标准程序库中的所有标识 ...
- using namespace std 和 using std::cin
相较using std::cin使用using namespace std不会使得程序的效率变低,或者稳定性降低,只是这样作会将很多的名字引入程序,使得程序员使用的名字集合变小,容易引起命名冲突. 在 ...
- namespace std
c++中使用namespace来防止命名冲突(重命名),我们经常使用的一些函数和变量都被放在一个叫std的namespace中,如标准I/O流操作,vector等等.我们在每一个文件中都可使用std中 ...
- C++ using namespace std(转载)
转载自http://www.kuqin.com/language/20080107/3532.html 感谢这位大神的解答! 以下的内容摘抄自转载的文章里面的部分内容. 早些的实现将标准库功能定义在全 ...
随机推荐
- wms-ssv数据字典
--------------------------------------------以下,托盘-- dbo.Container --托盘 , "托盘状态", "Con ...
- Splunk数据处理
0.提要 本篇主要从技术层面针对Splunk Enterprise中关于数据处理的概念.过程与部件进行了概要性总结. 1.数据管理基本概念 索引(index):Splunk用于存储事件的数据仓库: 索 ...
- html中块元素的居中。及兼容性
块在块中垂直居中(父元素postion:relative; 子元素position:absolute; top:50%; margin-top:负二分之一高度) 块在块中水平居中 (子元素marg ...
- Linux学习笔记之(2)~linux目录树概览和说明
献给知道mono,了解Jexus,对.net混搭技术感兴趣的朋友. linux目录树如下: 详解:/bin: 系统有很多放置执行档的目录,但/bin比较特殊.因为/bin放置的是在单人维护模式下还能够 ...
- web.xml配置文件中async-supported报错解决
项目中配置spring时async-supported报错: 是因为<async-supported>true</async-supported>是web.xml 3.0的新特 ...
- 网站部署中遇到的问题-未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项
问题描述: 运行站点抛出错误:未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项 原因: 应用程序池没有启用32位程序. 解决方法: 找到站点对应的应用程序池,设置启用32 ...
- .net iis excel导出问题
碰到几个问题的解决方法 1.当我远程服务器时才可以导出excel!!关闭了远程就不行... 解决:运行mmc -32组件服务 ->DCOM Config->Microsoft Excel ...
- @RequestBody与serialize()、serializeArray()、拼接Json 妙用总结
@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容, 比如说:application/json或者是app ...
- thinkphp更新数据库的时候where('')为字符串
if($user->where('phone='.$phone)->save($dataList)){} if($user->where(array('phone' =>$ph ...
- Hibernate Annotation (Hibernate 注解)
简介: 传统上,Hibernate的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射文件,并且在启动时进行加载. 然而现在借助新的 Hibernate Annotation 库, ...