赞
踩
当有要写的函数与STL里面重名时,如果不加作用域的话会报如下错误。
先说明下代码:
- template<typename T>
- void swap(T &a, T &b){
- cout << "调用了自己写的" << endl;
- T c = a;
- a = b;
- b = c;
- }
-
- void main(){
- int a = 1, b = 2;
- cout << "交换前:a:" << a << " b:" << b << endl;
- swap(a, b);
- cout << "交换后:a:" << a << " b:" << b << endl;
- getchar();
- }
运行截图如下
这里的解决方法有很多,一个是注释掉using namespace std;
还有一个就是加上::这个作用域,如下面这个代码
- #include <iostream>
- using namespace std;
-
- template<typename T>
- void swap(T &a, T &b){
- cout << "调用了自己写的" << endl;
- T c = a;
- a = b;
- b = c;
- }
-
- void main(){
- int a = 1, b = 2;
- cout << "交换前:a:" << a << " b:" << b << endl;
- ::swap(a, b);
- cout << "交换后:a:" << a << " b:" << b << endl;
- getchar();
- }

运行截图如下:
如果要用std就是std::swap
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。