Somthing wrong with my typedef struct.

This is a discussion on Somthing wrong with my typedef struct. within the C++ Programming forums, part of the General Programming Boards category; When I use this script: Code: typedef struct tagWINDOWS { HWND DesktopHwnd; HWND Current; } WINDOWS, *WINDOWS; I get these ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Exclamation Somthing wrong with my typedef struct.

    When I use this script:
    Code:
    typedef struct tagWINDOWS {
    	HWND DesktopHwnd;
    	HWND Current;
    } WINDOWS, *WINDOWS;
    I get these errors comping up at the ending bracket of my struct:
    conflicting declaration 'typedef struct tagWINDOWS*WINDOWS'
    'WINDOWS' has a previous declaration as `typedef struct tagWINDOWS WINDOWS'
    declaration of `typedef struct tagWINDOWS*WINDOWS'
    conflicts with previous declaration `typedef struct tagWINDOWS WINDOWS'
    declaration of `typedef struct tagWINDOWS*WINDOWS'
    conflicts with previous declaration `typedef struct tagWINDOWS WINDOWS'
    [Build Error] [PR.o] Error 1
    But, when I use this script:
    Code:
     
    typedef struct tagWINDOWS {
    	HWND DesktopHwnd;
    	HWND Current;
    }
    I get these errors coming up with the begining bracket of the first function I declare after my struct:
    new types may not be defined in a return type
    extraneous `int' ignored
    invalid function declaration
    [Build Error] [PR.o] Error 1
    What am I doing wrong?

    Thanks in advance, August.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    The syntax for typedef is:
    typedef existing newalias, newalias2, newaliasn;

    What do you expect typedef existing newalias, *newalias; to do?

    Structs and typedefs also end in a ;

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Also, typedef'ing a struct is not necesary in C++, struct foo is the same as foo. Secondly using a typedef to hide a pointer is particularly evil, unless you have a really good design reason for it (which I would love to hear), please don't do it.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    In c++ programs typedef is unnecessary other than to rename something. Example: the following two are identical.
    Code:
    typedef struct WINDOWS
    {
      // blabla
    } WINDOWS;
    
    struct WINDOWS
    {
      // blabla
    };
    so you might want something like this
    Code:
    struct WINDOWS {
    	HWND DesktopHwnd;
    	HWND Current;
    };
    
    typedef WINDOWS* ptrWINDOWS;
    >using a typedef to hide a pointer is particularly evil
    I hate that too but a lot of programmers (including M$) do it.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by Orbitz
    Also, typedef'ing a struct is not necesary in C++, struct foo is the same as foo. Secondly using a typedef to hide a pointer is particularly evil, unless you have a really good design reason for it (which I would love to hear), please don't do it.
    Why is it particularly evil?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Well, why not explain to me why you'd want to. What does hiding the fact that you have a pointer object do for you? You have constructors and destructors in C++ so you can handle creation and deletion of the obejct in there so you don't have to create explicit functions, (see FILE in C).

    So if you can answer why you'd want to hide the fact that it's a pointer then by all means go for it, however it seems more of a nusance to me rather than helpful.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    To be honest, I didn't realize I was hiding it, because I have never used structs before, this is my first attempt!

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by orbitz
    Well, why not explain to me why you'd want to. What does hiding the fact that you have a pointer object do for you? You have constructors and destructors in C++ so you can handle creation and deletion of the obejct in there so you don't have to create explicit functions, (see FILE in C).

    So if you can answer why you'd want to hide the fact that it's a pointer then by all means go for it, however it seems more of a nusance to me rather than helpful.
    I agree. I always find myself hunting around for what those damned things are -- how they are defined. Just using a plain-old every-day ordinary pointer makes the program a great deal easier to read.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    Look at this line:
    Code:
    WINDOWS, *WINDOWS
    You have two variables named WINDOWS (one pointer, one struct).
    They have conflicting names...

    Also you dont need a typedef at all, in c++ you dont need to say "struct" before each struct object (but in c you do). I see that your doing win32api, if you compiled it in c then just change the variable names:
    Code:
    typedef struct {
    	HWND DesktopHwnd;
    	HWND Current;
    } WINDOWS, *ptrWINDOWS;
    or in c++:
    Code:
    struct tagWINDOWS {
    	HWND DesktopHwnd;
    	HWND Current;
    } WINDOWS, *ptrWINDOWS;
    Last edited by JoshR; 09-13-2005 at 08:28 PM.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    this has been addressed in a previous post but apparently needs to be restarted here:

    STRUCTS AND CLASSES ARE THE EXACT SAME THING.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. weird typedef struct problems
    By olidem in forum C Programming
    Replies: 3
    Last Post: 07-28-2008, 02:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21