array of typedef struct declared inside class

Hi everyone!

I'm encountering problem about typedef struct. The case was i declared a struct inside a class like this:

class Myclass

typedef struct MysampleStruct{
int x;
int y;

If I'm going to use this struct like this:

Mysample[0].x = 1;

I'm having an error like "expected unqualified-id '[' token".

But if I'm gonna delete the 'typedef' in 'typedef struct MysampleStruct', its working fine.

Can someone explain to me why these happen?

Thanks in advance.
Just drop the keyword 'typedef'. It's completely unnecessary when it comes to declaring structs, and, in this case, is just wrong.
Last edited on
Hi Sir Cubbi. I'm new using this implementation, what does typedef do here? why am i having that kind of error? Thanks in advance
typedef here generates an error.

In general, typedef creates a type alias:

struct A {...}; // creates the type "A"
A s1; // creates an object of type A;
typedef A B; // creates the type "B" as an alias of "A"
B s2; // creates an object of type B (or A, they are the same) 

You're allowed to create a type and its alias simultaneously:

typedef struct A {...} B; // creates two aliased types "A" and "B"
A s1;
B s2; // same as A s2; 

You're allowed to create a type and construct an array of objects of that type simultaneously:

struct A {...} array[3]; // creates the type A and constructs an array of three As

But you can't create a type, create a type alias, and construct an array of objects of that type all on the same line.
Last edited on
Topic archived. No new replies allowed.