views:

57

answers:

2

I'm new to Objective-C so I'm using a book to get to grips with it. I'm at a bit where it's explaining structs and I can't for the life of me get them to work.

I have the following code:

int main (int argc, char *argv[])
{

    struct node
    {
        int nodeID;
        int x;
        int y;
        BOOL isActive;
    };

    typedef struct node myNode;

    myNode.nodeID = 1;

}

and I'm getting the error written in the title. Every time I search for this error online I found different variations such as 'before '>' token' or 'before '}' token' but i can't find anything with the '.' token and it's really frustrating and I assume it's somethings ridiculously trivial and basic. Any help would be appreciated.

+2  A: 

I believe you're trying to modify the actual type itself. nodeA is now the type of that struct, much like int. You need to do something like nodeA myNode, then you would be able to perform myNode.nodeID = 1 without error.

DarthShrine
Thanks for the fast reply. However doing that produces a similar error: expected '=', ',', ';', 'asm' or '__attribute__' before 'myNode'
Zachary Markham
It has just occurred to me that I have failed to mention I am doing this with the iPhone SDK, it didn't occur to me that this might make a difference. Does it?
Zachary Markham
Could you perhaps post the code surrounding the offending line, or perhaps the function containing it?
DarthShrine
That comment just fixed it for me, I wasn't declaring all of this within a function, it was just sat in my .m file after @implementation. I feel slightly dim now haha. Thanks for all your help! :)
Zachary Markham
Okay that still didn't fix it, I've edited the original post to include the function.
Zachary Markham
A: 

I've got it sorted now, I used the following and it seems to be fixed now:

int main (int argc, char *argv[])
{    

    struct node
    {
        int nodeID;
        int x;
        int y;
        BOOL isActive;
    };

    struct node myNode;

    myNode.nodeID = 1;
    myNode.x = 100;
    myNode.y = 200;
    myNode.isActive = TRUE;

}

Thanks for all your help Darth! :)

Zachary Markham
Glad you could figure it out (mostly) by yourself! Always handy in the learning process.
DarthShrine