登录 | 首页 -> 华新鲜事 -> 求学狮城 | 切换到:传统版 / sForum | 树形列表
A C++ problem about array
<<始页  [1]  末页>> 

A C++ problem about arrayI 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
[joyce (5-13 16:09, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

what is this for?int max_element();
[MrDJay (5-13 16:30, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

(引用 MrDJay:what is this for?int max_element(); )guess it is the problem[MrDJay (5-13 16:33, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼

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;
}




[我行故我 (5-13 17:40, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼

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 <iostream> than <iostream.h>
[香陵居士 (5-13 19:18, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼

(引用 香陵居士:You should declare the function before you use itmain() is an exception. All the other functions need to be declared before use...)is it not backward compatible de mah?not very sure, please teach me more :)[MrDJay (5-14 21:42, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼

(引用 MrDJay: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++.[香陵居士 (5-15 5:28, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼

(引用 香陵居士: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 :$[MrDJay (5-15 9:18, Long long ago)] [ 传统版 | sForum ][登录后回复]8楼

(引用 香陵居士:You should declare the function before you use itmain() is an exception. All the other functions need to be declared before use...)只要把下面的函数体整个放到main前面就行了。不一定非要declare。[Livecn (5-16 3:29, Long long ago)] [ 传统版 | sForum ][登录后回复]9楼

(引用 Livecn:只要把下面的函数体整个放到main前面就行了。不一定非要declare。)也算是declare了函数定义前面也给出了prototype的啊!

一般的做法还是先declare,后implement。特别是C++。
[香陵居士 (5-16 5:55, Long long ago)] [ 传统版 | sForum ][登录后回复]10楼

This program is not pure C++[Flying (5-18 13:21, Long long ago)] [ 传统版 | sForum ][登录后回复]11楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 求学狮城 | [刷新本页] | 切换到:传统版 / sForum