Can't typedef a struct

Discussion in 'C++' started by desktop, Sep 25, 2007.

  1. desktop

    desktop Guest

    I have this in a .h file which I include in a .cpp file:

    typedef struct {
    double x;
    double y;
    } pointz;

    But when I compile I get:

    graphics/debug.h:13: error: conflicting declaration ‘typedef struct
    pointz pointz’
    graphics/debug.h:13: error: ‘pointz’ has a previous declaration as
    ‘typedef struct pointz pointz’

    But I have not previously declared 'points'.
     
    desktop, Sep 25, 2007
    #1
    1. Advertising

  2. desktop

    Ian Collins Guest

    desktop wrote:
    > I have this in a .h file which I include in a .cpp file:
    >
    > typedef struct {
    > double x;
    > double y;
    > } pointz;
    >

    In C++, don't use typedef.

    struct pointz
    {
    double x;
    double y;
    };

    --
    Ian Collins.
     
    Ian Collins, Sep 25, 2007
    #2
    1. Advertising

  3. desktop

    Jim Langston Guest

    "Ian Collins" <> wrote in message
    news:...
    > desktop wrote:
    >> I have this in a .h file which I include in a .cpp file:
    >>
    >> typedef struct {
    >> double x;
    >> double y;
    >> } pointz;
    >>

    > In C++, don't use typedef.
    >
    > struct pointz
    > {
    > double x;
    > double y;
    > };


    In addition, it may be you are tying to include this twice without include
    guards.
     
    Jim Langston, Sep 25, 2007
    #3
  4. desktop

    desktop Guest

    Ian Collins wrote:
    > desktop wrote:
    >> I have this in a .h file which I include in a .cpp file:
    >>
    >> typedef struct {
    >> double x;
    >> double y;
    >> } pointz;
    >>

    > In C++, don't use typedef.
    >
    > struct pointz
    > {
    > double x;
    > double y;
    > };
    >


    with

    struct pointz {
    double x;
    double y;
    };

    Then I just get:


    graphics/debug.h:10: error: redefinition of ‘struct pointz’
    graphics/debug.h:10: error: previous definition of ‘struct pointz’

    even though its not been declared before.
     
    desktop, Sep 25, 2007
    #4
  5. desktop

    Ian Collins Guest

    desktop wrote:
    > Ian Collins wrote:
    >> desktop wrote:
    >>> I have this in a .h file which I include in a .cpp file:
    >>>
    >>> typedef struct {
    >>> double x;
    >>> double y;
    >>> } pointz;
    >>>

    >> In C++, don't use typedef.
    >>
    >> struct pointz
    >> {
    >> double x;
    >> double y;
    >> };
    >>

    >
    > with
    >
    > struct pointz {
    > double x;
    > double y;
    > };
    >
    > Then I just get:
    >
    >
    > graphics/debug.h:10: error: redefinition of ‘struct pointz’
    > graphics/debug.h:10: error: previous definition of ‘struct pointz’
    >
    > even though its not been declared before.


    Then you have done what Jim speculated and included a header without
    include guards twice.

    --
    Ian Collins.
     
    Ian Collins, Sep 25, 2007
    #5
  6. desktop

    Jim Langston Guest

    "desktop" <> wrote in message
    news:fdafhl$ci4$-c.dk...
    > Ian Collins wrote:
    >> desktop wrote:
    >>> I have this in a .h file which I include in a .cpp file:
    >>>
    >>> typedef struct {
    >>> double x;
    >>> double y;
    >>> } pointz;
    >>>

    >> In C++, don't use typedef.
    >>
    >> struct pointz
    >> {
    >> double x;
    >> double y;
    >> };
    >>

    >
    > with
    >
    > struct pointz {
    > double x;
    > double y;
    > };
    >
    > Then I just get:
    >
    >
    > graphics/debug.h:10: error: redefinition of ‘struct pointz’
    > graphics/debug.h:10: error: previous definition of ‘struct pointz’
    >
    > even though its not been declared before.


    Try this.

    #ifndef POINTZ_DEF
    #define POINTZ_DEF

    struct pointz {
    double x;
    double y;
    };

    #endif

    If that works, that means you are including the header twice without include
    guards. Whcih are somethign like this:

    myheader.h:
    #ifndef MYHEADER_H
    #define MYHEADER_H

    // header goes here

    #endif
     
    Jim Langston, Sep 25, 2007
    #6
  7. On Sep 25, 1:06 pm, desktop <> wrote:
    > Ian Collins wrote:
    > > desktop wrote:
    > >> I have this in a .h file which I include in a .cpp file:

    >
    > >> typedef struct {
    > >> double x;
    > >> double y;
    > >> } pointz;

    >
    > > In C++, don't use typedef.

    >
    > > struct pointz
    > > {
    > > double x;
    > > double y;
    > > };

    >
    > with
    >
    > struct pointz {
    > double x;
    > double y;
    >
    > };
    >
    > Then I just get:
    >
    > graphics/debug.h:10: error: redefinition of 'struct pointz'
    > graphics/debug.h:10: error: previous definition of 'struct pointz'
    >
    > even though its not been declared before.- Hide quoted text -
    >
    > - Show quoted text -


    Possibly a Header problem :(

    Karthik Balaguru
     
    karthikbalaguru, Sep 25, 2007
    #7
  8. desktop wrote:
    > I have this in a .h file which I include in a .cpp file:


    In a .cpp or in at least another .h file and the .cpp file?

    You probably need multiple inclusion guards.
     
    Juha Nieminen, Sep 25, 2007
    #8
  9. desktop

    BobR Guest

    Jim Langston wrote in message...
    >
    > If that works, that means you are including the header twice without

    include
    > guards. Whcih are somethign like this:
    >
    > myheader.h:
    > #ifndef MYHEADER_H
    > #define MYHEADER_H
    >
    > // header goes here
    >
    > #endif


    Add:

    Do yourself a BIG favor and comment all #endif.

    #endif // #ifndef MYHEADER_H

    When you get to the bottom of a file and see 20 or 30 #endif, it is hard to
    know which is ending what.

    My GCC(MinGW) system headers all have the comments, and it really helps when
    reading them (understanding them is a whole other thing! <G>).

    --
    Bob R
    POVrookie
     
    BobR, Sep 26, 2007
    #9
  10. desktop wrote:
    > graphics/debug.h:10: error: redefinition of ‘struct pointz’
    > graphics/debug.h:10: error: previous definition of ‘struct pointz’
    >
    > even though its not been declared before.


    Notice that the error lines say the previous definition is the same
    file:line as the redefinition. This means the file graphics/debug.h is
    being included multiple times.

    As other people on this thread have noted, you should use inclusion
    guards to prevent multiple includes from causing definitions to be seen
    twice.

    samuel
     
    R Samuel Klatchko, Sep 26, 2007
    #10
  11. desktop

    Stefan Naewe Guest

    On 9/26/2007 11:37 PM, BobR wrote:
    > Jim Langston wrote in message...
    >> If that works, that means you are including the header twice without

    > include
    >> guards. Whcih are somethign like this:
    >>
    >> myheader.h:
    >> #ifndef MYHEADER_H
    >> #define MYHEADER_H
    >>
    >> // header goes here
    >>
    >> #endif

    >
    > Add:
    >
    > Do yourself a BIG favor and comment all #endif.
    >
    > #endif // #ifndef MYHEADER_H
    >
    > When you get to the bottom of a file and see 20 or 30 #endif, it is hard to
    > know which is ending what.


    Why would you have that many #endif's at the bottom of a file ?


    > My GCC(MinGW) system headers all have the comments, and it really helps when
    > reading them (understanding them is a whole other thing! <G>).



    Regards,
    Stefan
    --
    Stefan Naewe stefan dot naewe at atlas-elektronik dot com
    Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
    Plain text mails only, please http://www.expita.com/nomime.html
     
    Stefan Naewe, Sep 27, 2007
    #11
  12. desktop

    James Kanze Guest

    On Sep 26, 11:37 pm, "BobR" <> wrote:
    > Jim Langston wrote in message...


    > > If that works, that means you are including the header twice
    > > without include guards. Whcih are somethign like this:


    > > myheader.h:
    > > #ifndef MYHEADER_H
    > > #define MYHEADER_H


    > > // header goes here


    > > #endif


    > Add:


    > Do yourself a BIG favor and comment all #endif.


    > #endif // #ifndef MYHEADER_H


    Why bother? There should only be one. Other than as include
    guards, conditional compilation is really something to be
    avoided.

    > When you get to the bottom of a file and see 20 or 30 #endif,
    > it is hard to know which is ending what.


    If you get to the bottom of a file, and see 20 or 30 #endif,
    it's easy to know that the system should be reengineered.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 27, 2007
    #12
  13. desktop

    Ian Collins Guest

    James Kanze wrote:
    >
    > If you get to the bottom of a file, and see 20 or 30 #endif,
    > it's easy to know that the system should be reengineered.
    >

    Something we agree on!

    --
    Ian Collins.
     
    Ian Collins, Sep 27, 2007
    #13
  14. desktop

    BobR Guest

    Stefan Naewe wrote in message...
    > On 9/26/2007 11:37 PM, BobR wrote:
    > > Jim Langston wrote in message...
    > >> If that works, that means you are including the header twice without

    > > include
    > >> guards. Whcih are somethign like this:
    > >>
    > >> myheader.h:
    > >> #ifndef MYHEADER_H
    > >> #define MYHEADER_H
    > >> // header goes here
    > >> #endif

    > >
    > > Add:
    > >
    > > Do yourself a BIG favor and comment all #endif.
    > >
    > > #endif // #ifndef MYHEADER_H
    > >
    > > When you get to the bottom of a file and see 20 or 30 #endif, it is hard

    to
    > > know which is ending what.

    >
    > Why would you have that many #endif's at the bottom of a file ?


    I do NOT! But, I've seen it.

    And I agree with you, James and Ian.

    --
    Bob R
    POVrookie
     
    BobR, Sep 27, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    505
  2. DanielEKFA
    Replies:
    8
    Views:
    616
    DanielEKFA
    May 16, 2005
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. Steven T. Hatton
    Replies:
    2
    Views:
    681
  5. oor
    Replies:
    0
    Views:
    1,357
Loading...

Share This Page