Typedef union. Problem mit instanzen
-
Hallo,
ich habe mehrere neuen Typen mit unions erstellt. Die sind in einer Headerdatei gespeichert. Aber wenn ich eine instanz dieses types in meiner Main-Funktion erstelle, bekomme ich 2 verschiedene Fehlermeldungen.
union T_FLOAT var;
da bekomme ich die Fehlermeldung:
error: storage size of 'var' isn't known
T_FLOAT var
bekomme ich die Meldung:
error: 'T_FLOAT' undeclared (first use in this function)
Hier der Code:
main.c
`#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "types.h"
int main()
{
T_FLOAT var; return 0;
}`
und types.h
`#ifndef TYPES_H#define TYPES_H
#include <inttypes.h>
typedef union _T_INT{
int v; uint8_t a[sizeof(int)];
};
typedef union T_FLOAT{
float v; uint8_t a[sizeof(float)];
}T_FLOAT;
typedef union T_LONG{
long v; uint8_t a[sizeof(long)];
};
#endif //TYPES_H`
Wenn ich das #ifndef usw. weglasse, funktioniert es wunderbar. Da die types.h später in mehrere Files eingebunden wird, muss das aber sein, denn sonst gibts da wieder Fehler wegen neu definition.
Ich nutze Codeblocks. Bei google gesucht habe ich schon, und auch viel gefunden, aber irgentwie hab ichs trotzdem nicht hinbekommen.
Danke schonmal im voraus.
MfG
Philipp
-
ISO/IEC 9899:TC3 schrieb:
7.1.3 Reserved identifiers
1 Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.
— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).
— All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.160)
— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
2 No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.
3 If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.
-
Hast du bei dir auf der Platte (Include-Verzeichnisse) auch mal nach types.h gesucht?
Evtl. existiert schon ein <types.h> vom Compiler (du schreibst leider nicht welcher) und die Headerdateien vom Compiler binden diese schon ein.
Und nach Murphy hat sie die gleichen Include Guards.Versuch mal
#ifndef MY_TYPES_H_ #define MY_TYPES_H_
-
Hi,
danke jetzt gehts. Ich nutze MinGW und eine types.h war tatsächlich schon vorhanden.
MfG
Philipp
-
Das bloße Vorhandensein einer
<types.h>
wäre nicht das Problem, wenn Du nicht reservierte Bezeichner verwenden würdest...