找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1844|回复: 5

[求助]编译出错,帮一下.

[复制链接]
发表于 2009-11-4 01:41:37 | 显示全部楼层 |阅读模式 IP:江苏扬州
#include<iostream.h>
template<class Type,int Rows,int Cols>
void sumAll(Type data[][Cols],Type result[])
{
for(int i=0;i<Rows;i++)
{
result[i]=0;
for(int j=0;j<Cols;j++)
result[i]+=data[i][j];
}
}
void main()
{
int d[][3]={{1,2,3},{2,3,4},{2,5,3},{3,5,9},{8,9,4}};
int r[5];
sumAll<int,5,3>(d,r);//必需强制实例转化
cout<<r[0]<<' '<<r[1]<<' '<<r[2]<<' '<<r[3]<<' '<<r[4]<<endl;
double dd[][4]={{1.2,1.3,5.2,3.3},{2.2,1.4,0.4,4.2},{7.2,2.3,8.2,5.8},{2.4,3.5,5.4,6.3}};
double rr[4];
sumAll<double,4,4>(dd,rr);//必需强制实例转化
cout<<rr[0]<<' '<<rr[1]<<' '<<rr[2]<<' '<<rr[3]<<endl;
}

error C2087: '<Unknown>' : missing subscript
发表于 2009-11-4 01:41:38 | 显示全部楼层 IP:江苏扬州
不能那么用啊
你要那么用的话,得动态分配内存
回复

使用道具 举报

发表于 2009-11-4 01:41:38 | 显示全部楼层 IP:江苏扬州
太高深了```
我没有接触到template```
帮不了``
回复

使用道具 举报

发表于 2009-11-4 01:41:40 | 显示全部楼层 IP:江苏扬州
#include <iostream>
#include <vector>
using namespace std;
template<class type>
void sumAll(vector< vector<type> > vvec,vector<type> &vec)
{
for(size_t i = 0; i < vvec.size(); i++)
{
type result = 0;
for(size_t j = 0; j < vvec[i].size(); j++)
result += vvec[i][j];
vec.push_back(result);
}
}
int main()
{
vector< vector<int> > ivvec;
vector<int> ivec1,ivec2,ivec3;
ivec1.push_back(1);
ivec1.push_back(2);
ivec1.push_back(3);
ivec2.push_back(2);
ivec2.push_back(3);
ivec2.push_back(4);
ivvec.push_back(ivec1);
ivvec.push_back(ivec2);
sumAll(ivvec,ivec3);
for(size_t size = 0; size < ivec3.size(); size++)
cout<<ivec3[size]<<" ";
cout<<endl;
system("pause");
return 0;
}
回复

使用道具 举报

发表于 2009-11-4 01:41:41 | 显示全部楼层 IP:江苏扬州
以下是引用wangxiang在2006-12-16 23:16:12的发言:
不能那么用啊
你要那么用的话,得动态分配内存
能说一下原因吗?谢谢.
回复

使用道具 举报

发表于 2009-11-4 01:41:43 | 显示全部楼层 IP:江苏扬州
不好意思,我错了,是可以那么用的
看来你用的是VC6.0换个DEV—CPP吧
你的程序没错
#include<iostream>
using namespace std;
template<class Type,int Rows,int Cols>
void sumAll(Type data[][Cols],Type result[])
{
for(int i=0;i<Rows;i++)
{
result[i]=0;
for(int j=0;j<Cols;j++)
result[i]+=data[i][j];
}
}
int main()
{
int d[][3]={{1,2,3},{2,3,4},{2,5,3},{3,5,9},{8,9,4}};
int r[5];
sumAll<int,5,3>(d,r);//必需强制实例转化
cout<<r[0]<<' '<<r[1]<<' '<<r[2]<<' '<<r[3]<<' '<<r[4]<<endl;
double dd[][4]={{1.2,1.3,5.2,3.3},{2.2,1.4,0.4,4.2},{7.2,2.3,8.2,5.8},{2.4,3.5,5.4,6.3}};
double rr[4];
sumAll<double,4,4>(dd,rr);//必需强制实例转化
cout<<rr[0]<<' '<<rr[1]<<' '<<rr[2]<<' '<<rr[3]<<endl;
system("pause");
return 0;
}
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 03:22 , Processed in 0.243058 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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