 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Stefan Guest
|
Posted: Thu Dec 30, 2004 8:15 pm Post subject: Trying to make a structure external |
|
|
Hi,
I am trying to get my structure to be externed so I can use it in all
my
projects source files, but the code is generating errors. Here is some
of my
code:
"Globals.cpp"
typedef struct SPECKEY {
bool BeenUsed;
bool Pressed;
bool On;
} specialkey;
#include "globals.h"
specialkey IsShift, IsAlt, IsMod1, IsMod2, IsMod3; // line 24.
....
"Globals.h"
....
extern specialkey IsAlt;
extern specialkey IsShift;
extern specialkey IsMod1;
extern specialkey IsMod2;
extern specialkey IsMod3;
....
It generates the following errors:
Globals.h(77): error C2146: syntax error : missing ';' before
identifier
'IsAlt'
Globals.h(77): error C2501: 'IsAlt' : missing storage-class or type
specifiers
etc...
And the same errors for the other extern lines. I have tried leaving
out the
'specialkey' bit of the extern lines, but that generates the following
errors:
KeyEventSink.cpp(142): error C2228: left of '.On' must have
class/struct/union type
KeyEventSink.cpp(143): error C2228: left of '.Pressed' must have
class/struct/union
type
Globals.cpp(24): error C2371: 'IsShift' : redefinition; different basic
types
etc...
Does anyone know how I can make a structure externed without generating
errors? Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Fri Dec 31, 2004 1:22 pm Post subject: Re: Trying to make a structure external |
|
|
Stefan wrote:
| Quote: | I am trying to get my structure to be externed so I can use it
in all my projects source files, but the code is generating
errors.
|
Strictly speaking, in C++, all class names (except for classes
defined in a function) are "external". Of course, the
compilation unit which uses the name has to know what it means.
| Quote: | Here is some of my code:
"Globals.cpp"
typedef struct SPECKEY {
bool BeenUsed;
bool Pressed;
bool On;
} specialkey;
|
This looks very much like C. In C++, it would be more idiomatic
to write:
struct specialKey
{
bool BeenUsed ;
bool Predded ;
bool On ;
} ;
It would also be more idiomatic to place this in a header file.
C depends on hiding such information in an implementation file,
whereas in C++, if you don't want others to use it, you declare
it private.
| Quote: | #include "globals.h"
|
What's in the header file?
| Quote: | specialkey IsShift, IsAlt, IsMod1, IsMod2, IsMod3; // line 24.
...
"Globals.h"
|
Ditto...
| Quote: | ...
extern specialkey IsAlt;
extern specialkey IsShift;
extern specialkey IsMod1;
extern specialkey IsMod2;
extern specialkey IsMod3;
...
It generates the following errors:
Globals.h(77): error C2146: syntax error : missing ';' before
identifier
'IsAlt'
Globals.h(77): error C2501: 'IsAlt' : missing storage-class or type
specifiers
etc...
And the same errors for the other extern lines.
|
I'm not too clear as to the context, but I suspect that the
problem is that you are including these definitions somewhere
where specialKey is not known to the compiler. If all you want
to do is to make the globals known, without doing anything with
them other than passing their address to a function, or
something similar, then adding:
struct specialKey ;
to the top of Globals.h should do the trick.
| Quote: | I have tried leaving out the 'specialkey' bit of the extern
lines, but that generates the following errors:
KeyEventSink.cpp(142): error C2228: left of '.On' must have
class/struct/union type
KeyEventSink.cpp(143): error C2228: left of '.Pressed' must have
class/struct/union
type
Globals.cpp(24): error C2371: 'IsShift' : redefinition; different basic
types
etc...
|
I'm now totally confused as to what you are trying to do. If
you simply omit the word specialKey in the extern declaration,
you should get an immediate error there.
| Quote: | Does anyone know how I can make a structure externed without
generating errors? Thanks.
|
As I said at the start, classes (which includes structures) are
automatically global symbols, unless the class is defined within
a function. On the other hand, you do have to tell the compiler
that the symbol exists, and you have to give it a minimum of
information concerning the symbol. Otherwise, the compiler
can't know what the symbol is. In many cases, it is sufficient
that the compiler know it is a class type, without any
additional information. But if you want to use any of the
members (or have an actual object of the type, as opposed to
just manipulating pointers or references initialized elsewhere),
then you have to provide the compiler with a complete class
definition.
--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Birbacher Guest
|
Posted: Sat Jan 01, 2005 1:13 am Post subject: Re: Trying to make a structure external |
|
|
Hi!
Stefan wrote:
| Quote: | Does anyone know how I can make a structure externed without generating
errors? Thanks.
|
You need to put the class definition into the header. Like this:
// "Globals.h"
struct specialkey
{
bool BeenUsed, Pressed, On;
};
extern specialkey IsShift, IsAlt, IsMod1, IsMod2, IsMod3;
// "Globals.cpp"
#include "Globals.h"
specialkey IsShift, IsAlt, IsMod1, IsMod2, IsMod3;
....
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|