This is my problem: variable-sized object ‘Feld’ may not be initialized

Hello,

I am new to programming and kind of stuck on this assignment.

This is the code that I wrote so far:
#include <iostream>
using namespace std;

int Feld [11][11];
int main()
{
for (int Reihe = 0; Reihe <= 10; Reihe++)
{
for (int Zeile = 0; Zeile <= 10; Zeile++)
{
int Feld1[Reihe][Zeile] = Reihe * Zeile;
}
}
return 0;
}

and this results in the following error message:

tobias@Lonestar:~/c++$ g++ nl.cpp -Wall
nl.cpp: In function ‘int main()’:
nl.cpp:11:37: error: variable-sized object ‘Feld’ may not be initialized
nl.cpp:11:8: warning: unused variable ‘Feld’ [-Wunused-variable]

At the end of the day, I tried to work around that and declared the array in the following way:

const int AR = 11;
const int AZ = 11;
int Feld [AR][AZ];

This leads pretty much to the same error message.

Any ideas? Help is appreciated Thanks!

Tobias

I think the error is generated due to a typo

int Feld [11][11];
....

int Feld1[Reihe][Zeile] = Reihe * Zeile;

You should write


Feld[Reihe][Zeile] = Reihe * Zeile;

Also it is better to set the for statement the following way

for (int Reihe = 0; Reihe < 11; Reihe++)
Last edited on
The problem is this:
1
2
3
4
for (int Zeile = 0; Zeile <= 10; Zeile++)
{
int Feld1[Reihe][Zeile] = Reihe * Zeile; // This declares a new array, hence Feld isn't used (it should emit an error because Reihe and Zeile are not const)
}


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Thanks Vlad for your quick response!

There was indeed a typo, like I said, I was messing around with the code for a while, but now that I have changed it, I still get the same error message.

This is how the code looks like right now and afterwards I post the error message:

int Feld [11][11];
int main()
{
for (int Reihe = 0; Reihe < 11; Reihe++)
{
for (int Zeile = 0; Zeile < 11; Zeile++)
{
int Feld[Reihe][Zeile] = Reihe * Zeile;
}
}

nl.cpp: In function ‘int main()’:
nl.cpp:11:37: error: variable-sized object ‘Feld’ may not be initialized
nl.cpp:11:8: warning: unused variable ‘Feld’ [-Wunused-variable]
Look again on your statement and compare it with may statement showed above

int Feld[Reihe][Zeile] = Reihe * Zeile;
Hi timeout2575

one small thing the may help one day - put the -Wextra flag on your command line

g++ nl.cpp -Wall -Wextra

This outputs extra warnings (yes even more than all)

You can use the -o flag to specify a name for the executable, rather than the default a.out

Hope this helps.
Thanks Vlad! After I removed the INT it worked fine! Thanks! Hopefully I will get better at this soon!
Thanks to everyone else that responded! :)
Topic archived. No new replies allowed.