Help with struct

Hello,

i have the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

struct fun{
       int v[100];
};

using namespace std;

int main(void){
        
        
     for(int i=0;i<100;i++)
             fun.v[i] = i;   

     for(int i=0;i<100;i++)
             cout << fun.v[i] << "\n";   
             
    cout << "\n\n";
    system("PAUSE");
    return 0;
}


but i receive the compiler errors:
13 expected primary-expression before '.' token 
16 expected primary-expression before '.' token


How can i solve it, please ?

Thank you.
These lines:
1
2
3
struct fun{
       int v[100];
};

define fun as a type, not a variable. You are getting the error because you are trying to access (non static) members of a class/struct type from it's name rather than a particular instance of it.

You should make an instance of type fun:
fun myFun;,
just as you would make an instance of type int (int myInt;).

If you want, you can merge the struct's definition with declaring an instance of that type:
1
2
3
struct myStruct {
// ...
} myVar;

This code defines a type: 'myStruct' and creates a variable of that type, 'myVar'.
fun is a like a data type.
To solve it you must define a variable of type fun.

Add this line in main ()

fun any_name_you_want
and then substitute fun.v with:
any_name_you_want

EDIT: Ah! Too late!
Last edited on
Topic archived. No new replies allowed.