鱼C论坛

 找回密码
 立即注册
查看: 2150|回复: 3

函数模板问题!请高手解答!!

[复制链接]
发表于 2012-2-28 23:04:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
using namespace std;

template<typename T>;
void rant(T p[],T s)
{
        int temp;

        for(int i=0;i<s-1;i++)
                for(int j=i+1;j<s;j++)
                {
                        if(p[i]>p[j])
                        {
                                temp=p[i];
                                p[i]=p[j];
                                p[j]=temp;
                        }
                }

                for(int f=0;f<s;f++)
                        cout<<p[f]<<endl;
}

int main()
{
        int a[100];

        float b[100];

        double c[100];

        int n;

        cout<<"please input the amount of numbers:"<<endl;

    cin>>n;

        for(int i=0;i<n;i++)
                cin<<a[i];

        rant(a,n);

        for(int j=0;j<n;j++)
                cin<<b[j];

        rant(b,n);

        for(int k=0;k<n;k++)
                cin<<c[k];

        rant(c,n);
        return 0;
}

例题.cpp
e:\作业\练习\引用\例题.cpp(4) : error C2059: syntax error : '<end Parse>'
e:\作业\练习\引用\例题.cpp(6) : error C2143: syntax error : missing ';' before '{'
e:\作业\练习\引用\例题.cpp(6) : error C2447: missing function header (old-style formal list?)
e:\作业\练习\引用\例题.cpp(34) : fatal error C1903: unable to recover from previous error(s); stopping compilation

问题:不明白是怎么回事?提示也不知道在说什么!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-2-28 23:43:49 | 显示全部楼层
看不懂函数  ,。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-2-29 11:21:43 | 显示全部楼层
  1. #include<iostream>
  2. using namespace std;

  3. template<typename T>
  4. void rant(T p[],int s)
  5. {
  6.         int temp;

  7.         for(int i=0;i<s-1;i++)
  8.                 for(int j=i+1;j<s;j++)
  9.                 {
  10.                         if(p[i]>p[j])
  11.                         {
  12.                                 temp=p[i];
  13.                                 p[i]=p[j];
  14.                                 p[j]=temp;
  15.                         }
  16.                 }

  17.                 for(int f=0;f<s;f++)
  18.                         cout<<p[f]<<endl;
  19. }

  20. int main()
  21. {
  22.         int a[100];

  23.         float b[100];

  24.         double c[100];

  25.         int n;

  26.         cout<<"please input the amount of numbers:"<<endl;

  27.     cin>>n;

  28.         for(int i=0;i<n;i++)
  29.                 cin>>a[i];

  30.         rant(a,n);

  31.         for(int j=0;j<n;j++)
  32.                 cin>>b[j];

  33.         rant(b,n);

  34.         for(int k=0;k<n;k++)
  35.                 cin>>c[k];

  36.         rant(c,n);
  37.         return 0;
  38. }
复制代码
输入:
3
2 1 3
回排序输出为1 2 3
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-3-5 22:49:24 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 14:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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