Error: variable or array declared void???

Hello!
I have this program to generate a 2DD array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
srand (time(NULL));


int m[5][3];


for (int i=0;i<5;i++){
     for (int j=0;j<3;j++){
              m[i][j]=rand()% 10;
              cout<<m[i][j]<<" ";
              
     }
cout<<endl;
}


return 0;
}

 



Now, I wanted to sent it to function, and it gave errror:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<stdlib.h>
using namespace std;

void Add(d1[5][3]){
for (int i=0;i<5;i++){
     for (int j=0;j<3;j++){
              d1[i][j]=rand()% 10;
              cout<<m[i][j]<<" ";
              
     }
cout<<endl;
}


}


int main(){


int m[5][3];

cout<<Add(m[5][3]);
}
    


Line 7: error: variable or field 'Add' declared void


(p.s. Any better indentation now?)

Many thanks!
1) At line 24, you're attempting to output the value returned by Add(m[5][3]). But there is no value returned by Add, because you've declared it void.

2) At line 24, m[5][3] is beyond the end of the array. Arrays are indexed starting from 0, so the last element in your array is m[4][2].

3) At line 24, even if those were valid array indices, m[5][3] would be a single integer. Your Add function has been defined to take an array, not an integer.

4) In your definition of Add, you haven't specified the type of the array you're passing as an argument.
Last edited on
Hello!

I made some corrections, now the last error ( I hope):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<stdlib.h>
using namespace std;

void Add(int d1[5][3]){
for (int i=0;i<5;i++){
     for (int j=0;j<3;j++){
              d1[i][j]=rand()% 10;
              cout<<d1[i][j]<<" ";
              
     }
cout<<endl;
}


}


int main(){


int m[5][3];

cout<<Add(m);
}



What is this error telling now:

In function 'int main()':
Line 26: error: no match for 'operator<<' in 'std::cout << Add(((int (*)[3])(& m)))'



Many thanks!!!
cout<<Add(m);

should be :

Add( m );
Last edited on
What is this error telling now:

In function 'int main()':
Line 26: error: no match for 'operator<<' in 'std::cout << Add(((int (*)[3])(& m)))'


It's a mistake I already explained as point 1 in my previous post.
Hello, All!

Does it mena, if function Add is VOID, we do not write cout in main, but if it was f.e. int Add, then we could have cou<<Add(m);

or :

x=Add(m);
cout<<x;


???
Many thanks!
it means that the function doesn't return anything, that's why cout can't display it on the screen...

from my opinion, it's just doesn't depend on the function's return type but also from your returned value (i.e. if you doesn't return a value, you will encounter an error)
Topic archived. No new replies allowed.