LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 815|回复: 1

请教:函数二义性问题,谢谢

[复制链接]
发表于 2008-7-5 22:49:45 | 显示全部楼层 |阅读模式
下面是Linux下一段代码,报出错误:call of overloaded 'swap(int &, int&)' is ambiguous 等等
同样我把代码写在VC++6.0上也报出错误:ambiguous call to overloaded function
希望大家帮我看下,如何解决呢,谢谢大家。
code in VC++6.0 (在Linux下头文件需要指明路径):

#include <iostream>

template <class Any>
void swap(Any &a, Any &b);

int main()
{
    using namespace std;
    int i= 10;
    int j= 20;
    cout << "Using complier-generatd int swapper.\n";
    swap(i, j);
    cout << "i, j = " << i << ", " << j << ".\n";

    double x= 24.5;
    double y= 81.7;
    cout << "Using complier-generatd double swapper.\n";
    swap(x, y);
    cout << "x, y = " << x << ", " << y << ".\n";

    return 0;
}

template <class Any>
void swap(Any &a, Any &b)
{
    Any temp;
    temp= a;
    a= b;
    b= temp;
}
发表于 2008-7-6 02:23:08 | 显示全部楼层
STL 裏面已經有這樣的一個 swap 了,你再定義一個,compiler 當然不知道該用哪個好,用 g++ 的話可參看 /usr/include/c++/4.2/bits/stl_algobase.h
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表