expected unqualified-id error

hello,
I have this testing code:

#include <iostream>

using namespace std;

class hstates
{
public:
double p[3][3];
int n;
hstates(int s){n=s;}
for(int i=0; i<=2; i++) {
for(int j=0; j<=2; j++) {
stari.p[i][j]=1/s;
}
}
};

main()
{
int a=rand();
hstates stari(4);
}

I get these errors:
test.cpp:18: error: expected unqualified-id before ‘for’
test.cpp:18: error: ISO C++ forbids declaration of ‘i’ with no type
test.cpp:18: error: expected ‘;’ before ‘<=’ token
test.cpp:18: error: ISO C++ forbids declaration of ‘i’ with no type
test.cpp:18: error: expected ‘;’ before ‘++’ token
test.cpp:33: error: expected ‘;’ at end of input
test.cpp:33: error: expected ‘}’ at end of input
test.cpp:16: error: expected unqualified-id at end of input.

I am getting crazy looking for the answer :) (not much experience in C++)
Thanks for any help,

Bogdan
hstates(int s){n=s;} would be called a local function if it were allowed. It's not, so it's called a syntax error.

Ignore all that... The code's not in a function.
Last edited on
closed account (jwC5fSEw)
Well, you're not seeding your rand function or even including the library that uses rand. I don't know why you're not getting an error there.

Both hstates and main have no return value. hstates should return void and main always needs to return int.

Also, in your inner for-loop, you're using stari, which isn't declared anywhere in your class. I'm guessing you meant to just use p?
Topic archived. No new replies allowed.