LinuxSir.cn,穿越时空的Linuxsir!

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

GCC4.0是不是不支持模板?

[复制链接]
发表于 2006-10-13 23:04:22 | 显示全部楼层 |阅读模式
想问一下,GCC4.0是不是不支持模板?代码和错误提示如下:
//funtemp.cpp --using a function template
#include<iostream>

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

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

  double x=24.5,y=81.7;
  cout<<"x,y="<<x<<","<<y<<".\n";
  cout<<"Using comiler-generated double swapper: \n";
  swap(x,y);
  cout<<"Now x,y="<<x<<","<<y<<".\n";

  cin.get();
  return 0;
}

template <class T>
void swap(T &a,T &b)
{
  T temp;
  temp=a;
  a=b;
  b=temp;
}
funtemp.cpp: In function ‘int main()’:
funtemp.cpp:13: 错误:调用重载的 ‘swap(int&, int&)’ 有歧义
funtemp.cpp:5: 附注:备选为: void swap(T&, T&) [with T = int]
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/stl_algobase.h:92:
附注:         void std::swap(_Tp&, _Tp&) [with _Tp = int]
funtemp.cpp:19: 错误:调用重载的 ‘swap(double&, double&)’ 有歧义
funtemp.cpp:5: 附注:备选为: void swap(T&, T&) [with T = double]
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/stl_algobase.h:92:
附注:         void std::swap(_Tp&, _Tp&) [with _Tp = double]
发表于 2006-10-13 23:29:20 | 显示全部楼层
你觉得可能么
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-13 23:47:07 | 显示全部楼层
我也觉得不太可能,但总提示:定义的函数歧义,
回复 支持 反对

使用道具 举报

发表于 2006-10-14 00:02:42 | 显示全部楼层
贴下代码吧.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-14 01:24:52 | 显示全部楼层
//funtemp.cpp --using a function template
#include<iostream>

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

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

  double x=24.5,y=81.7;
  cout<<"x,y="<<x<<","<<y<<".\n";
  cout<<"Using comiler-generated double swapper: \n";
  swap(x,y);
  cout<<"Now x,y="<<x<<","<<y<<".\n";

  cin.get();
  return 0;
}

template <class T>
void swap(T &a,T &b)
{
  T temp;
  temp=a;
  a=b;
  b=temp;
}
funtemp.cpp: In function ‘int main()’:
funtemp.cpp:13: 错误:调用重载的 ‘swap(int&, int&)’ 有歧义
funtemp.cpp:5: 附注:备选为: void swap(T&, T&) [with T = int]
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/stl_algobase.h:92:
附注:         void std::swap(_Tp&, _Tp&) [with _Tp = int]
funtemp.cpp:19: 错误:调用重载的 ‘swap(double&, double&)’ 有歧义
funtemp.cpp:5: 附注:备选为: void swap(T&, T&) [with T = double]
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/stl_algobase.h:92:
附注:         void std::swap(_Tp&, _Tp&) [with _Tp = double]
回复 支持 反对

使用道具 举报

发表于 2006-10-14 02:44:53 | 显示全部楼层
swap是stl已经定义了的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-14 09:44:51 | 显示全部楼层
问题已经解决了,谢谢楼上的!Thank you very much!
回复 支持 反对

使用道具 举报

发表于 2006-10-15 00:39:21 | 显示全部楼层
学习,学习!
回复 支持 反对

使用道具 举报

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

本版积分规则

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