Jirikai Naito
Jirikai Naito

Reputation: 5

c++ Structure Arrays - "This declaration has no storage class or type specifier"

I'm having trouble with struct arrays. this is the code I have that produces 2 red error lines.

    struct frequents
{
    int count;
    char letter;
};

frequents testArray[2];
testArray[1].letter = 'v';   

The error appears under testArray which has the declaration error and under '.' which mentions that it expected a ';'.

Upvotes: 0

Views: 1137

Answers (2)

Ron
Ron

Reputation: 15541

This is a statement:

testArray[1].letter = 'v';

Statements are to be executed inside functions (function body) and not in some arbitrary global namespace as you have it now. Move your statement inside a main program entry point function, a lambda, a free standing function or a class member function body.

Alternatively, use the aggregate initialization to initialize your array:

frequents testArray[2] = {{ 1, 'a' }, { 2, 'b' }};

or without the extra braces:

frequents testArray[2] = { 1, 'a', 2, 'b' };

Upvotes: 3

Olivier Sohn
Olivier Sohn

Reputation: 1322

Because you're not in a function: replace

testArray[1].letter = 'v';

by

int main() {
    testArray[1].letter = 'v';
    return 0;
}

and it compiles allright.

Upvotes: 1

Related Questions