Using Definitons

M

Matthias Matker

Hello!

I am sure you can help me with tis question:

I am writing a programme which uses severall classes, but I have a
"shared" file which contains definitions used by several classes.

---- define.h ----

const unsigned short TBufCharSize = 256;

typedef struct TEntry
{
unsigned short uAge;
bool bUseIt;
};

....

---


The methods of the classes use this definitions

myClass::doIt(TEntry e);
anotherClass:doThat(TEntry e);

Therefore I have to add #include "define.h" in the Files "myClass.h"
"anotherClass.h" and "main.cpp"

But the compilers causes an error: "TBufCharSize redefined, TEntry
redefined".

Can anyone tell me how to use "shared" definitions correctly?

Thanks, Merry Christmas...
 
M

mlimber

Matthias said:
Hello!

I am sure you can help me with tis question:

I am writing a programme which uses severall classes, but I have a
"shared" file which contains definitions used by several classes.

---- define.h ----

const unsigned short TBufCharSize = 256;

typedef struct TEntry
{
unsigned short uAge;
bool bUseIt;
};

...

---


The methods of the classes use this definitions

myClass::doIt(TEntry e);
anotherClass:doThat(TEntry e);

Therefore I have to add #include "define.h" in the Files "myClass.h"
"anotherClass.h" and "main.cpp"

But the compilers causes an error: "TBufCharSize redefined, TEntry
redefined".

Can anyone tell me how to use "shared" definitions correctly?

Thanks, Merry Christmas...

In your header, use include guards to prevent the file contents from
being included multiple times:

#if !defined( INCLUDED_DEFINE_H )
# define INCLUDED_DEFINE_H
// Your code here
#endif

Cheers! --M
 
?

=?ISO-8859-15?Q?Stefan_N=E4we?=

Matthias said:
Hello!

I am sure you can help me with tis question:

I am writing a programme which uses severall classes, but I have a
"shared" file which contains definitions used by several classes.

---- define.h ----

const unsigned short TBufCharSize = 256;

typedef struct TEntry
{
unsigned short uAge;
bool bUseIt;
};

...

---


The methods of the classes use this definitions

myClass::doIt(TEntry e);
anotherClass:doThat(TEntry e);

Therefore I have to add #include "define.h" in the Files "myClass.h"
"anotherClass.h" and "main.cpp"

But the compilers causes an error: "TBufCharSize redefined, TEntry
redefined".

Can anyone tell me how to use "shared" definitions correctly?

Thanks, Merry Christmas...

Hint: "include guards"

#ifndef INCLUDE_FILE_H
#define INCLUDE_FILE_H
//..code of include_file.h
#endif

(of course INCLUDE_FILE_H needs to be different for each .h file)


Stefan
 
T

Torsten Wiebesiek

Stefan said:
Hint: "include guards"

#ifndef INCLUDE_FILE_H
#define INCLUDE_FILE_H
//..code of include_file.h
#endif

(of course INCLUDE_FILE_H needs to be different for each .h file)

Using 'include guards' is highly recomended, but it doesn't completely solve
your problem.

'Include guards' make sure, your header files are included only once in each
source file. If you have more than one source file including define.h, the
global variable TBufCharSize will be defined more than once. This results
in a linking error: "multiple definition of `TBufCharSize'"

The header define.h should only contain a declaration:
extern const unsigned short TBufCharSize;

The definition should be placed into a source file, e.g. define.c:
const unsigned short TBufCharSize = 256;

Be aware of the difference between 'declaration' and 'definition'.

Torsten
 
M

Matthias Matker

Torsten Wiebesiek schrieb:

Hello Torsten!
Using 'include guards' is highly recomended, but it doesn't completely solve
your problem.

'Include guards' make sure, your header files are included only once in each
source file. If you have more than one source file including define.h, the
global variable TBufCharSize will be defined more than once. This results
in a linking error: "multiple definition of `TBufCharSize'"

The header define.h should only contain a declaration:
extern const unsigned short TBufCharSize;

The definition should be placed into a source file, e.g. define.c:
const unsigned short TBufCharSize = 256;

Can I handle the struct TEntry in the same way?
 
D

Default User

Matthias said:
Torsten Wiebesiek schrieb:

Hello Torsten!


Can I handle the struct TEntry in the same way?

No, TEntry is a declaration, not a definition. You aren't clear yet
about the difference between types and objects. You need to study those
concepts in your book until you are clear, otherwise you'll have no end
of difficulty.



Brian
 
H

hacker++

Torsten said:
Stefan said:
Matthias Matker wrote:
I am writing a programme which uses severall classes, but I have a
"shared" file which contains definitions used by several classes.

---- define.h ----

const unsigned short TBufCharSize = 256;
[snip]
But the compilers causes an error: "TBufCharSize redefined, TEntry
redefined".

Can anyone tell me how to use "shared" definitions correctly?

Thanks, Merry Christmas...

Using 'include guards' is highly recomended, but it doesn't completely solve
your problem.

'Include guards' make sure, your header files are included only once in each
source file. If you have more than one source file including define.h, the
global variable TBufCharSize will be defined more than once. This results
in a linking error: "multiple definition of `TBufCharSize'"

The header define.h should only contain a declaration:
extern const unsigned short TBufCharSize;

The definition should be placed into a source file, e.g. define.c:
const unsigned short TBufCharSize = 256;

Be aware of the difference between 'declaration' and 'definition'.

Torsten

There is no need to use "extern" for constants. A better way is to use
a anonymous namespace.

------define.h-----

namespace // anonymous
{
const unsigned short TBufCharSize = 256;
}
 
J

Jack Klein

Using 'include guards' is highly recomended, but it doesn't completely solve
your problem.

'Include guards' make sure, your header files are included only once in each
source file. If you have more than one source file including define.h, the
global variable TBufCharSize will be defined more than once. This results
in a linking error: "multiple definition of `TBufCharSize'"

The header define.h should only contain a declaration:
extern const unsigned short TBufCharSize;

That's not necessary at all. Const objects defined at namespace
scope, even in the global namespace (since the C++ standard no longer
uses the term 'file scope') have internal linkage by default.
The definition should be placed into a source file, e.g. define.c:
const unsigned short TBufCharSize = 256;

Not necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top