Re: Problem declaring struct
Re: Problem declaring struct
- Subject: Re: Problem declaring struct
- From: Aaron Montgomery <email@hidden>
- Date: Sat, 12 May 2007 16:49:43 -0700
On May 12, 2007, at 4:27 PM, Livio Isaia wrote:
I have to declare a struct type in the header of a "cpp" file, so I
type for example:
struct myData {
int a;
in b;
long c;
};
but the compiler tells me;
"redefinition of 'struct myData'
previous definition of 'struct myData'"
(which correspond to the same file line "struct myData {").
Do you have include guards on your headers? You only get to declare a
struct once, so if the same header file is included into a source
file more than once (possibly through a series of includes), then the
compiler is seeing the struct more than once. A standard idiom for C+
+ header file "myData.h" would be something like:
#ifndef INCLUDE_GUARD_myData
#define INCLUDE_GUARD_myData
at the top of the file and
#endif // INCLUDE_GUARD_myData
at the bottom of the file. This way the preprocessor will skip the
contents if it has already seen them. There is also a
#pragma once
that you can use telling the preprocessor to only process the
contents of the file once. The pragma is not portable (I use include
guards, so it might not even be available in gcc), but may allow the
preprocessor to skip the header file if it sees it again (rather than
opening it and then skipping the contents. I don't know gcc's policy
on this.
Hope that helps,
Aaron
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden