Results 1 to 5 of 5

Thread: array of typedef struct declared inside class

  1. #1
    Join Date
    Nov 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question 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
    {
    private:

    typedef struct MysampleStruct{
    int x;
    int y;
    }Mysample[3];
    }


    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: array of typedef struct declared inside class

    If you use typedef then you need to pass the new name too (which you didn't). But in general in C++ typedefing structs doesn't make sense since structs are equivalent to classes so you can access the struct using just its name (without prepending it with 'struct').
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: array of typedef struct declared inside class

    Quote Originally Posted by wysota View Post
    If you use typedef then you need to pass the new name too (which you didn't). .
    Hi Sir, about the quoted above? How will I pass the new name? Sorry if this sounds dumb. I'm new here Sir. Thanks a lot

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: array of typedef struct declared inside class

    Qt Code:
    1. struct MySampleStruct {
    2. int x;
    3. int y;
    4. };
    5.  
    6. MySampleStruct MySample[3];
    To copy to clipboard, switch view to plain text mode 

    or possibly even:

    Qt Code:
    1. struct {
    2. int x;
    3. int y;
    4. } MySample[3];
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd November 2012 at 06:17.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: array of typedef struct declared inside class

    When typedef is used the struct definition should follow with the new typename. You are specifing the variable (array) instead of typename. Correct way to specify is below. Also typedef does not have much use in C++ (as explained by wysota), unless you are porting C based code, and don,t want to change it.

    Qt Code:
    1. class Myclass
    2. {
    3. private:
    4. struct MysampleStruct{
    5. int x;
    6. int y;
    7. }Mysample[3];
    8. };
    9. // or
    10. class Myclass
    11. {
    12. private:
    13. typedef struct MysampleStruct{
    14. int x;
    15. int y;
    16. }MysampleStructType;
    17.  
    18. MysampleStructType Mysample[3];
    19. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Pass struct array to function
    By willbeas_SMU in forum General Programming
    Replies: 2
    Last Post: 19th March 2012, 17:05
  2. Struct in a class
    By Atomic_Sheep in forum Newbie
    Replies: 6
    Last Post: 10th February 2012, 09:34
  3. Unable to initialize char [] var inside struct ....
    By tonnot in forum General Programming
    Replies: 1
    Last Post: 26th August 2011, 10:40
  4. Struct Parsing, multiple name typedef failing
    By Syndacate in forum Qt Tools
    Replies: 1
    Last Post: 12th July 2011, 07:47
  5. Struct into a C++ class
    By franco.amato in forum General Programming
    Replies: 24
    Last Post: 30th September 2010, 16:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.