Need Some Help With a Function

I can't seem to figure out what is wrong with the snippet below. It works in Java with a simple class surrounding it. What is wrong here? I am using GNU GCC with Code:Blocks. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main() {
    int array [] = {1,2,3};

    for (int i = 0; i < sizea (array); i++) { // output contents of array
        cout << array [i] + "\n";
    }
    return 0;
}

int sizea (int [] array){ // returns the size of the array
    return (sizeof (array) / sizeof (int));
}



Here's the error log.



/home/marvel/CPPTest/main.cpp||In function ‘int main()’:|
/home/marvel/CPPTest/main.cpp|8|error: ‘sizea’ was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|




To me it seems as though passing the array into the size (int[] array) function didn't work.

Thank you in advance.
Last edited on
It works in Java
... yes, but this is C++.
1. At the point you call size(), you haven't declared a prototype. So the compiler doesn't know what it is.
2. Perhaps you meant int size (int array[]). array is an incomplete type and is treated as type int*. The return value becomes sizeof(int*)/sizeof(int), which isn't correct.
3. A for loop has three parts, you've only specified 2.
closed account (zvRX92yv)
There is no standard called size().

When you do something like this -
'a' + 'b'

It adds the ASCII equivalent of 'a' and 'b'. If they were 10 and 11 respectively, you will get 21.

Integers aren't the same as strings or characters.

cout converts an integer to a string automatically for you, and you're supposed to separate your integers and strings and everything with <<.

Same thing with cin, but use >>.

Example -
cout << "IT'S OVER " << 9000 << "!!";



A for loop is supposed to be like this -
for ( initialization; condition; command )

The initialization is done only once, when the loop starts. Usually, variables are declared there.

The condition is checked if it's true. If it is, it continues the loop.

The loop is done afterwards.

Then the command is executed, then it checks the condition and if it's true, it does the loop again.

You can skip the initialization or the command if you want, but you must keep the semi-colons, here's an example -

for ( ; condition; )



EDIT: It doesn't know that sizea() exists until AFTER main().

You have two options -
A - Put sizea() before main()
B - Make a function prototype.

A function prototype is like the declaration of a function before it's implemented..

Put the prototype before main().

A prototype looks like this -
itsOver( int number );

for a function that looks like -
1
2
3
4
itsOver( int number )
{
    return number;
}
Last edited on
Thank you very much. It worked!
@Nahiyan: Nice DBZ reference.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int sizea (int array []){ // returns the size of the array
    return (sizeof (array) / sizeof (int));
}

int main() {
    int array [] = {1,2,3};

    for (int i = 0; i < sizea (array); i++) { // output contents of array
        cout << array [i] <<" \n";
    }
    return 0;
}


closed account (zvRX92yv)
No problem.

BTW it's really in-efficient, since sizea() is called everytime.
A constant is better, really.
It worked!
You see all 3 numbers? I don't think so.
@kbw You're right. Would you please explain why this is?
See point 2 in my post above. If that isn't clear, I can elaborate.
1.you should declare the function sizea() before use it.

2.sizeof() is not properly used here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int sizea (int array []){ // returns the size of the array

	cout<<sizeof(array)<<endl;   //ouput 4

    return (sizeof (array) / sizeof (int));
}

int main() {
    int array [] = {1,2,3};

	cout<<(sizeof (array) / sizeof (int))<<endl;   //ouput 3
	cout<<sizea(array)<<endl;                      //ouput 1

    for (int i = 0; i < sizea (array); i++) { // output contents of array
        cout << array [i] <<endl;
    }
    return 0;
}



pay attention to the three cout i add.The result may inply that when you call function sizea(array),it just pass the pointer of array to the function.
Last edited on
Topic archived. No new replies allowed.