You are not logged in.

1

Wednesday, August 24th 2005, 9:00am

Easy question

Why does my compiler choke on the following line, saying:
"# Size of array must be an integral constant expression of value
greater than 0."?

The cout prints out as 2, 3, 4, whatever...when i comment out said line.

Source code

1
2
3
4
5
6
    cout << "NumRows: " << numRows() << endl;
    QString tempstr[numRows()];/*************THIS LINE
    for(int i = 0; i < numRows(); i++)
    {
      tempstr[i] = QString(item(i,0)->text());
    }
[TTGUY] kr

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

2

Wednesday, August 24th 2005, 9:21am

RE: Easy question

numRows() probably returns int. And an int can be 0 or less, so you might end up getting a negative size of an array.

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

3

Wednesday, August 24th 2005, 10:18am

No, it means that constructing an array on the stack is only supported for constant array sizes.

For example

Source code

1
2
3
4
int foo[10]; // OK, 10 is a literal, thus constant

int len = 10;
int bar[len]; // not OK, len is a variable, thus not constant


If you need a variable array do this

Source code

1
int* array = new int[len];

and don't forget to delete[] it later

Cheers,
_
Qt/KDE Developer
Debian User

hatulflezet

Professional

Posts: 1,301

Location: Munich Germany

Occupation: Programmer

  • Send private message

4

Wednesday, August 24th 2005, 10:23am

@anda_skoa
Thats what I remembered as well, but in order to be sure, I tested it.
My gcc has no problem with:
int i;
QString test;

So I guess its a compiler thing, some allow it, some dont...
Click here! I dare ya'!! :]
Project Archimedes
----------------------------------------------------------
"Don't panic, and thanks for all the fish!"

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Wednesday, August 24th 2005, 10:29am

It is a GNU extension to GCC to allow such constructs. For compilers that don't support such extensions the array size must be known at compile time, thus variables are not supported. I didn't notice the word "constant" in the error message, it suggests that this compiler doesn't support run time array sizing.

6

Wednesday, August 24th 2005, 10:33am

Yes, what you all say makes sense.
My code is now:

Source code

1
2
3
4
5
    QString* tempstr; 
    for(int i = 0; i < numRows(); i++)
    {
      tempstr[i] = QString(item(i,0)->text());//************
    }


But I get a bus error on the line inside the for loop??
[TTGUY] kr

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

7

Wednesday, August 24th 2005, 10:41am

You have a pointer to a QString Object and the thing you probably want is an array of QStrings:

Source code

1
2
3
4
5
if(numRows()<=0) return false;
QString *tempstr = new QString[numRows()];
for(int i = 0; i < numRows(); i++){
  tempstr[i] = item(i,0)->text();
}

beshop

Beginner

Posts: 1

Location: China

Occupation: Engineer

  • Send private message

8

Thursday, August 25th 2005, 3:17am

why don't you use QStringlist?