Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can't typedef a struct

Reply
Thread Tools

Can't typedef a struct

 
 
desktop
Guest
Posts: n/a
 
      09-25-2007
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'.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-25-2007
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.
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      09-25-2007
"Ian Collins" <ian-> 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.



 
Reply With Quote
 
desktop
Guest
Posts: n/a
 
      09-25-2007
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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-25-2007
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.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-25-2007
"desktop" <> wrote in message
news:fdafhl$ci4$...
> 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


 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 1:06 pm, desktop <f...@sss.com> 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

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-25-2007
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.
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      09-26-2007

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


 
Reply With Quote
 
R Samuel Klatchko
Guest
Posts: n/a
 
      09-26-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
typedef struct {} SName; vs. struct SName{}; Steven T. Hatton C++ 2 08-03-2005 09:59 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57