error message

I am getting a weird error message that says"invalid types 'int[int]' for array subscript", I am trying to make a function that prints out a multidimensional array. It's probably just a careless mistake but I'm am new to C++ and only 13 years old: here is the code:
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
27
28
29
30
31
// this is the header
#ifndef ARRAYF_H_
#define ARRAYF_H_
using namespace std;
void printArray(int array[],int arraySize)
{


    for(int x=0; x<arraySize; x++)
    {
        cout << array[x] << endl;
    }



}
void printmArray(int array[],int rows,int columns){

    for(int x=0;x<rows;x++){

        for(int y=0;y<columns;y++){
            cout << array << " ";
        }
        cout << endl;
    }


}

#endif
//end of code 

please help me
The code you posted looks fine as far as syntax goes. Perhaps the error is somewhere else.

I am getting a weird error message that says"invalid types 'int[int]' for array subscript"
Can you please post as output from the compiler. It's usually telling exactly what's wrong.

It's probably just a careless mistake but I'm am new to C++
We all have errors.

13 years old
There's no age discrimination here, we all started somewhere :)
Can you please post as output from the compiler. It's usually telling exactly what's wrong.
I don't know how to post output from the compiler, but I can tell you exactly what it says. It says
message: in function 'void printmArray(int*, int, int,)' line:22 Message: error: invalid types 'int[int]' for array subscript
=== Build finished 1 errors, 0 warnings ===

I think what I did was posting as output from the compiler, but I'm not sure...
Thanks for the positive reinforcement! :)
Last edited on
ok... I was able to copy and paste it exactlyas it was:
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h||In function 'void printmArray(int*, int, int)':|
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h|22|error: invalid types 'int[int]' for array subscript|
||=== Build finished: 1 errors, 0 warnings ===|
I thought it might be something to do with that line.

Shouldn't array be something like array[y]?
Your line 22 is outputting the memory location of the first element. Although that shouldn't be what's causing the problem.

Perhaps you should post the relevant section of the .cpp file also.
Last edited on
 
cout << array << " ";
is correct syntax even though it doesn't do what you intend.

You should fix it, but I don't know how you calculate and index from you row/column values.
yeah, I didn't even realize I already had it written array[x][y]
I don't know why I didn't put it on like that. here is the way is should be written
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
27
28
29
30
// this is the header
#ifndef ARRAYF_H_
#define ARRAYF_H_
using namespace std;
void printArray(int array[],int arraySize)
{


    for(int x=0; x<arraySize; x++)
    {
        cout << array[x] << endl;
    }



}
void printmArray(int array[],int rows,int columns){
    for(int x=0;x<rows;x++){

        for(int y=0;y<columns;y++){
            cout << array[x][y] << " ";
        }
        cout << endl;
    }


}

#endif

when I did this I got the same message
Last edited on
array is a pointer to an int.
array[x] is an int.
You can't use operator[] on an int so array[x][y] will not work.

The way to solve this depends on how you store your 2D array.

The way I prefer is to use a normal array of size rows * cols and use x + y * columns to index the correct element.

1
2
3
4
5
6
7
8
9
10
int array[rows * cols];

...

for(int x=0;x<rows;x++){
    for(int y=0;y<columns;y++){
        cout << array[x + y * columns] << " ";
    }
    cout << endl;
}

Last edited on
thanks! you have solved my Problem but now I'm getting a different message,
it says
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h||In function 'void printmArray(int*, int, int)':|
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h|18|error: declaration of 'int array [(((unsigned int)(((int)(rows * columns)) + -0x000000001)) + 1)]' shadows a parameter|
||=== Build finished: 1 errors, 0 warnings ===|
It has something to do with int array[rows * columns];
here is the code I have
1
2
3
4
5
6
7
8
9
10
11
12
13
void printmArray(int array[],int rows,int columns){
    int array[rows * columns];
    for(int x=0;x<rows;x++){
        for(int y=0;y<columns;y++){
        cout << array[x + y * columns] << " ";
    }
    cout << endl;
}




}

after this I should be fine
I mean you should declare the array as int array[rows * columns]; outside the function.
1
2
3
int array[rows * columns];
...
printmArray(array, rows, columns);


And remove int array[rows * columns]; from printmArray.
OK, I did that and it said that the integers 'rows' and 'columns' were not declared in this scope, so right above that I declared them. then it gave me this message
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h|19|error: array bound is not an integer constant|
||=== Build finished: 1 errors, 0 warnings ===|

I don't what this is and I have never seen it before
here is my code:
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
27
28
29
30
31
32
33
34
// this is the header
#ifndef ARRAYF_H_
#define ARRAYF_H_
using namespace std;
void printArray(int array[],int arraySize)
{


    for(int x=0; x<arraySize; x++)
    {
        cout << array[x] << endl;
    }



}
int rows;
int columns;
int array[rows * columns];
void printmArray(int array[],int rows,int columns){

    for(int x=0;x<rows;x++){
        for(int y=0;y<columns;y++){
        cout << array[x + y * columns] << " ";
    }
    cout << endl;
}




}

#endif 


I wonder if it's because I am using the same integers above it in the printArray function...
ok, just fixed that and it's not the problem
Last edited on
I'm sorry if I'm not very clear. It's not important what you call your array or how you create it. The important thing is that the size is rows * columns.
Sorry if I'm not clear. It's not important what you name the array or how it's created. The important thing is that it has rows * columns elements. You decide what rows and columns is for your array. If you don't dynamically allocate your array you have to use a constant size. rows and columns are not constants so that's why you got an error.
Last edited on
Ok I kind of see where your getting at now. so I am assuming that a constant size is just a value like: int a = 9. (please correct me if I am wrong.) If that is the case then a constant size will not work for me because the purpose of the function is to print out arrays of all different sizes, so that means I have to dynamically allocate my array, Could you please show me how to do that?
I have dynamically allocated my array(I think...). now it is giving me a message:
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h|18|error: expected constructor, destructor, or type conversion before '=' token|
||=== Build finished: 1 errors, 0 warnings ===|

I know that constructors have to do with classes, but I am not in a class so this doesn't make sense here is my code:
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
27
28
29
30
31
32
33
// this is the header
#ifndef ARRAYF_H_
#define ARRAYF_H_
using namespace std;
void printArray(int array[],int arraySize)
{


    for(int x1=0; x1<arraySize; x1++)
    {
        cout << array[x1] << endl;
    }



}
int *arrayx;
arrayx=new int[size]l 
void printmArray(int arrayx[],int rows,int columns){

    for(int x=0;x<rows;x++){
        for(int y=0;y<columns;y++){
        cout << arrayx[x + y * columns] << " ";
    }
    cout << endl;
}




}

#endif 

please help me
Last edited on
Even if the array has fixed size you can use the function on different arrays with different sizes.
1
2
3
4
5
6
7
int arr1[100];
printmArray(arr1, 10, 10);

const int rows = 25;
const int columns = 10;
int arr2[rows * columns];
printmArray(arr2, rows, columns);


Ok that makes sense, but it is still asking for a destructor or type conversion,
I still don't understand why or how I can have that,
again probably just a careless mistake.
Here is the error:
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h|18|error: expected constructor, destructor, or type conversion before '(' token|
C:\Users\Eamon\Documents\C++ programs\test\arrayf.h|23|error: expected constructor, destructor, or type conversion before '(' token|
||=== Build finished: 2 errors, 0 warnings ===|

here is the code:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
// this is the header
#ifndef ARRAYF_H_
#define ARRAYF_H_
using namespace std;
void printArray(int array[],int arraySize)
{


    for(int x1=0; x1<arraySize; x1++)
    {
        cout << array[x1] << endl;
    }



}
int arr1[100];
printmArray(arr1, 10, 10);

const int rows = 25;
const int columns = 10;
int arr2[rows * columns];
printmArray(arr2, rows, columns)
{

    for(int x=0;x<rows;x++){
        for(int y=0;y<columns;y++){
        cout << arr2[x + y * columns] << " ";
    }
    cout << endl;
}




}

#endif
Last edited on
How do you call the functions? This could be the reason!

In the main you declare your array and call the functions. It seems to me you do s.th. like printArray(myarray[0], 5); instead of printArray(myarray, 5);

You maybe want to put the code of the functions into the .cpp-file and just leave the prototypes in the header.
Last edited on
Topic archived. No new replies allowed.