A C++ problem about array
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 11 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:joyce (等级:3 - 略知一二,发帖:760) 发表:2003-05-13 16:09:20  楼主  关注此帖
A C++ problem about array
I am a beginner of C++ programming. When I am learning the array, I copied a program from a book and cannot be executed. I don't know what is wrong? Please help me!!!


#include<iostream.h>
void main()
{
int max_element();
int n, a[10];
int max;
for(n=0; n<10; n ++)
cin>>a[n];
max=max_element(a, 10);
cout<<max;
}
int array[], size;
int max_element(array,size)
{
int temp, j;
temp = array[0];
for(j=0; j<size; j++)
if(temp<array[j]) temp=array[j];
return temp;
}



The errors are
'max_element' : function does not take 2 parameters
<Unknown>' : function-style initializer appears to be a function definition
Put your OWN COOL signature here!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:MrDJay (等级:10 - 炉火纯青,发帖:10172) 发表:2003-05-13 16:30:42  2楼
what is this for?
int max_element();
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:MrDJay (等级:10 - 炉火纯青,发帖:10172) 发表:2003-05-13 16:33:29  3楼
what is this for?int max_element();
guess it is the problem
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:我行故我 (等级:5 - 略有小成,发帖:1333) 发表:2003-05-13 17:40:28  4楼
come in>>
do u mean to find the max number of an array whoes size is 10?
if so..
I changed ur program a bit..
u can see the difference..


#include<iostream.h>
int max_element(int[], int);
void main(){
int n, a[10];
int max;
for(n=0; n<10; n ++)
cin>>a[n];
max=max_element(a, 10);
cout<<max;
cout<<endl;
}
int max_element(int array[] , int size){
int temp, j;
temp = array[0];
for(j=0; j<size; j++)
if(temp<array[j])
temp=array[j];
return temp;
}




欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:香陵居士 (等级:16 - 好恐怖呀,发帖:22662) 发表:2003-05-13 19:18:51  5楼
You should declare the function before you use it
main() is an exception. All the other functions need to be declared before use. Just put your function prototype at the beginning of the program.

BTW: It's better to use <iostream> than <iostream.h>
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:MrDJay (等级:10 - 炉火纯青,发帖:10172) 发表:2003-05-14 21:42:38  6楼
You should declare the function before you use itmain() is an exception. All the other functions need to be declared before use. Just put your function prototype at the beginning of the program. BTW: It's better to use than (more...)
is it not backward compatible de mah?
not very sure, please teach me more :)
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:香陵居士 (等级:16 - 好恐怖呀,发帖:22662) 发表:2003-05-15 05:28:21  7楼
is it not backward compatible de mah?not very sure, please teach me more :)
It seems that you are really new to C/C++
Functions need to be declared before use, this is the basic rule for C/C++.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:MrDJay (等级:10 - 炉火纯青,发帖:10172) 发表:2003-05-15 09:18:36  8楼
It seems that you are really new to C/C++Functions need to be declared before use, this is the basic rule for C/C++.
thanks :$
always java oriented :$
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Livecn (等级:13 - 举世无双,发帖:9378) 发表:2003-05-16 03:29:25  9楼
You should declare the function before you use itmain() is an exception. All the other functions need to be declared before use. Just put your function prototype at the beginning of the program. BTW: It's better to use than (more...)
只要把下面的函数体整个放到main前面就行了。
不一定非要declare。
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:香陵居士 (等级:16 - 好恐怖呀,发帖:22662) 发表:2003-05-16 05:55:39  10楼
只要把下面的函数体整个放到main前面就行了。不一定非要declare。
也算是declare了
函数定义前面也给出了prototype的啊!

一般的做法还是先declare,后implement。特别是C++。
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2003-05-18 13:21:30  11楼
This program is not pure C++
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 11 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码