error message
Eamon McCarron (9)
Apr 5, 2012 at 11:54am UTC
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
kbw (5520)
Apr 5, 2012 at 12:04pm UTC
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.
There's no age discrimination here, we all started somewhere :)
Eamon McCarron (9)
Apr 5, 2012 at 12:26pm UTC
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 saysmessage: 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 Apr 5, 2012 at 12:27pm UTC
Eamon McCarron (9)
Apr 5, 2012 at 12:31pm UTC
ok... I was able to copy and paste it exactly as 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 ===|
iHutch105 (1079)
Apr 5, 2012 at 12:31pm UTC
I thought it might be something to do with that line.
Shouldn't array be something like array[y] ?
Krahl (61)
Apr 5, 2012 at 12:34pm UTC
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 Apr 5, 2012 at 12:39pm UTC
kbw (5520)
Apr 5, 2012 at 12:40pm UTC
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.
Eamon McCarron (9)
Apr 5, 2012 at 12:59pm UTC
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 written1 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 Apr 5, 2012 at 1:01pm UTC
Peter87 (3911)
Apr 5, 2012 at 1:11pm UTC
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 Apr 5, 2012 at 1:13pm UTC
Eamon McCarron (9)
Apr 5, 2012 at 2:36pm UTC
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 have1 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
Peter87 (3911)
Apr 5, 2012 at 2:51pm UTC
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.
Eamon McCarron (9)
Apr 5, 2012 at 2:57pm UTC
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 Apr 5, 2012 at 3:00pm UTC
Peter87 (3911)
Apr 5, 2012 at 3:23pm UTC
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 .
Peter87 (3911)
Apr 5, 2012 at 3:25pm UTC
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 Apr 5, 2012 at 3:26pm UTC
Eamon McCarron (9)
Apr 5, 2012 at 3:58pm UTC
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?
Eamon McCarron (9)
Apr 5, 2012 at 9:05pm UTC
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 Apr 5, 2012 at 9:05pm UTC
Peter87 (3911)
Apr 6, 2012 at 9:50am UTC
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);
Eamon McCarron (9)
Apr 6, 2012 at 1:26pm UTC
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 Apr 6, 2012 at 1:29pm UTC
frema (38)
Apr 6, 2012 at 1:53pm UTC
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 Apr 6, 2012 at 2:18pm UTC
Topic archived. No new replies allowed.