| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 414 人关注过本帖
标题:帮忙我解决error问题
只看楼主 加入收藏
晓宁
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2011-9-7
结帖率:71.43%
收藏
已结贴  问题点数:7 回复次数:2 
帮忙我解决error问题
//This program demonstrates function templates.
#include<iostream>、
using namespace std;

template<class Type>
Type Max(Type x,Type y)
{
    return(x>y)?x:y;
}

template<class Type>
void Swap(Type& x,Type y)
{
    Type temp;
    temp=x;
    x=y;
    y=temp;
}

template<class TypeA,class TypeB>
TypeA Func(TypeA x,TypeB y)
{
    TypeA r;
    r= 3*x + 2*y;
    return r;
}

template<class Type>
Type Sum(Type[],int n)
{
    Type t=0;
    for(int i=0;i<n;++i)
        t+=a[i];
    return t;
}

int main()
{
    int i=3,j=5;
    double d=8.62;,e=4.14;

    float f_arr[6]={1.2,2.3,3.4,4.5,5.6,6.7};
    int i_arr[4]={4,5,6,7};

    cout<<"i="<<i<<"and j="<<j<<endl;
    cout<<"d="<<d<<"and e="<<e<<endl<<endl;
    cout<<"The larger of i and j is "<<Max(i,j)<<endl;
    cout<<"The larger of dand e is "<<Max(d,e)<<endl<<endl;
    Swap(i,j);
    Swap(d,e);
    cout<<"i="<<i<<"and j="<<j<<endl;
    cout<<"d="<<d<<"and e="<<e<<endl<<endl;
    cout<<"Func() applied to i and d is"<<Func(i,d)<<endl;
    cout<<"Func() applied ro d and i is"<<Func(d,i)<<endl<<endl;
    cout<<"The sum of f_arr[] is "<<Sum(f_arr,6)<<endl;
    cout<<"The sum of i_arr[] is "<<Sum(i_arr,4)<<endl;
    return 0;
}
搜索更多相关主题的帖子: function include return 
2011-09-18 21:36
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:4 
楼下帮你,我路过。

离恨恰如春草,更行更远还生。
2011-09-18 21:41
tisyang
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:132
专家分:737
注 册:2011-5-7
收藏
得分:4 
程序代码:
//This program demonstrates function templates.
#include <iostream>
using namespace std;

template<class Type>
Type Max(Type x,Type y)
{
  return(x>y)?x:y;
}

template<class Type>
void Swap(Type& x,Type y)
{
  Type temp;
  temp=x;
  x=y;
  y=temp;
}

template<class TypeA,class TypeB>
TypeA Func(TypeA x,TypeB y)
{
  TypeA r;
  r= 3*x + 2*y;
  return r;
}

template<class Type>
Type Sum(Type *a,int n)
{
  Type t=0;
  for(int i=0;i<n;++i)
    t+=a[i];
  return t;
}

int main()
{
  int i=3,j=5;
  double d=8.62,e=4.14;

  float f_arr[6]={1.2,2.3,3.4,4.5,5.6,6.7};
  int i_arr[4]={4,5,6,7};

  cout<<"i="<<i<<"and j="<<j<<endl;
  cout<<"d="<<d<<"and e="<<e<<endl<<endl;
  cout<<"The larger of i and j is "<<Max(i,j)<<endl;
  cout<<"The larger of dand e is "<<Max(d,e)<<endl<<endl;
  Swap(i,j);
  Swap(d,e);
  cout<<"i="<<i<<"and j="<<j<<endl;
  cout<<"d="<<d<<"and e="<<e<<endl<<endl;
  cout<<"Func() applied to i and d is"<<Func(i,d)<<endl;
  cout<<"Func() applied ro d and i is"<<Func(d,i)<<endl<<endl;
  cout<<"The sum of f_arr[] is "<<Sum(f_arr,6)<<endl;
  cout<<"The sum of i_arr[] is "<<Sum(i_arr,4)<<endl;
  return 0;
}


注意书写的检查,头文件引用 #include 后需要空格
using namespace std 后的 分号 必须是英文标点。
Type Sum(Type[],int n)

这里建议不去使用 数组类型,改为指针可以提高适用范围。
double d=8.62;,e=4.14;
这里还是书写问题。。。。。。
自己多看看编译器提示的错误详细信息,一个一个去解决,培养自己解决问题的能力

C++ 用无参数构造函数生成对象时候请勿在构造函数后添加无用的那一对括号,否则有可能会被当成函数声明而忽略,嗯,栈上构建的时候就是这样。
2011-09-19 09:20
快速回复:帮忙我解决error问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020399 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved