Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Can't typedef a struct (http://www.velocityreviews.com/forums/t539793-cant-typedef-a-struct.html)

desktop 09-25-2007 07:27 AM

Can't typedef a struct
 
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'.

Ian Collins 09-25-2007 07:32 AM

Re: Can't typedef a struct
 
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.

Jim Langston 09-25-2007 07:35 AM

Re: Can't typedef a struct
 
"Ian Collins" <ian-news@hotmail.com> wrote in message
news:5lrrrhF9i6kuU4@mid.individual.net...
> 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.




desktop 09-25-2007 08:06 AM

Re: Can't typedef a struct
 
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.

Ian Collins 09-25-2007 08:09 AM

Re: Can't typedef a struct
 
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.

Jim Langston 09-25-2007 08:57 AM

Re: Can't typedef a struct
 
"desktop" <fff@sss.com> wrote in message
news:fdafhl$ci4$1@news.net.uni-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



karthikbalaguru 09-25-2007 10:41 AM

Re: Can't typedef a struct
 
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


Juha Nieminen 09-25-2007 11:17 AM

Re: Can't typedef a struct
 
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.

BobR 09-26-2007 09:37 PM

Re: Can't typedef a struct
 

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



R Samuel Klatchko 09-26-2007 10:03 PM

Re: Can't typedef a struct
 
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


All times are GMT. The time now is 10:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.