Thread: typedef issue

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    typedef issue

    hello!

    I'm writing a C++ program and the problem I face is when I used typedef for a srtucy as follows:

    Code:
    typedef struct{
    
    //declarations
    
    
    }Foo;
    The error I'm getting from the compiler is:

    redefinition; different basic types

    Maybe is something wrong because Im using typedef in C++?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    A simple:
    Code:
    struct Foo
    {
        //declarations
    };
    would be equivalent to that C construct.

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    ^ this.

    I think what you're trying to do is

    Code:
    typedef struct Foo
    {
    
    } Foo;
    You're trying to typedef the entire struct name. In C, in order to declare an instance of a struct, you'd have to say

    Code:
    struct Foo bar;
    So you want to typedef that entire name (struct Foo) into just the name of the struct (Foo)
    Last edited by Rodaxoleaux; 05-09-2012 at 07:39 AM.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by quo View Post
    redefinition; different basic types

    Maybe is something wrong because Im using typedef in C++?
    No, that looks right. Are you using Foo somewhere else already?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Some all compilers required it this way.

    Code:
    typedef struct Foo_tagname
    {
    
    } Foo;
    The structure name (often called tag name) and the type name needed to be different on some old compilers.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    116
    Thank you all very much for your answers!
    I tried it the simple way as follows:

    Code:
    struct Foo{
    
    //declarations
    
    };
    and it doesn't return that error any more(there used to be about 300 of them exactly the same)

    but it returns now:


    'struct' type redefinition

    What is that??

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    post the full error message.
    it should be something like
    Foo:'struct' type redefinition
    Kurt

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by ZuK View Post
    post the full error message.
    it should be something like
    Foo:'struct' type redefinition
    Kurt
    error C2011: 'Foo' : 'struct' type redefinition

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by quo View Post
    error C2011: 'Foo' : 'struct' type redefinition
    Post the exact error message and the line of code it says has the error.

    Then search for another place you define the same identifier.

    If the file is a header file (or is included by other files); please confirm it has header guards in it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That's what I thought.
    This is usually an indication that you #include a header multiple times.
    That header is propably missing the include guards.

    Kurt

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    116
    all I do in this header file Foo.h is:

    Code:
    #include<stdio.h>
    #include<iostream>
    
    
    struct Foo{
    	//declarations
    };
    nothing else.
    and then i include it wherever is needed..
    the error line is line with struct Foo
    and gives the error I mentioned above

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    try include guards like

    Code:
    #ifndef FOO_H_INCLUDED
    #define FOO_H_INCLUDED
    
    #include<stdio.h>
    #include<iostream>
     
     
    struct Foo{
        //declarations
    };
    
    #endif
    Kurt

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by ZuK View Post
    try include guards like

    Code:
    #ifndef FOO_H_INCLUDED
    #define FOO_H_INCLUDED
    
    #include<stdio.h>
    #include<iostream>
     
     
    struct Foo{
        //declarations
    };
    
    #endif
    Kurt
    It doesn't return the mistake now,but could you explain to me what do the guards do exactly?
    Now my Foo.h is included ok in the rest of the code(other .h or .cpp files include Foo.h)?

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by quo View Post
    It doesn't return the mistake now,but could you explain to me what do the guards do exactly?
    Now my Foo.h is included ok in the rest of the code(other .h or .cpp files include Foo.h)?
    The problem is that any class or struct may only be defined once in any tranlation unit ( one definition rule ).
    if you #include a header in other headers then it can happen that a single header is #included more then once. That is not allowed.
    Therefore you add includeguards.

    The first line ( #ifndef FOO_H_INCLUDED ) tells the preprocessor that the part up to #endif should only be processed if the symbol FOO_H_INCLUDED is not defined.
    The next line #defines ths symbol. If the same header is #included another time then the symbol is already defined and your code is skipped.

    Google include guards or header guards. I'm shure you will find better and more detailed explainations.

    Kurt

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by ZuK View Post
    The problem is that any class or struct may only be defined once in any tranlation unit ( one definition rule ).
    if you #include a header in other headers then it can happen that a single header is #included more then once. That is not allowed.
    Therefore you add includeguards.

    The first line ( #ifndef FOO_H_INCLUDED ) tells the preprocessor that the part up to #endif should only be processed if the symbol FOO_H_INCLUDED is not defined.
    The next line #defines ths symbol. If the same header is #included another time then the symbol is already defined and your code is skipped.

    Google include guards or header guards. I'm shure you will find better and more detailed explainations.

    Kurt
    Thank you very much!
    I understood the whole thing!
    You made it very clear

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. typedef issue
    By Drac in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2009, 04:41 PM
  3. typedef help
    By switchcase in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2007, 12:21 PM
  4. typedef
    By abhi_86 in forum C Programming
    Replies: 26
    Last Post: 04-11-2007, 03:20 PM
  5. ? about typedef
    By smd in forum C++ Programming
    Replies: 8
    Last Post: 07-23-2003, 10:49 AM