By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Using Definitons

P: n/a
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...
Dec 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Matthias Matker wrote:
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

Dec 22 '05 #2

P: n/a
Matthias Matker wrote:
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
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Dec 22 '05 #3

P: n/a
Stefan Nwe wrote:
Matthias Matker wrote:
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)


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
Dec 22 '05 #4

P: n/a
Torsten Wiebesiek schrieb:

Hello Torsten!
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)

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?

Dec 22 '05 #5

P: n/a
Matthias Matker wrote:
Torsten Wiebesiek schrieb:

Hello Torsten!
> 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)

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?


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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 22 '05 #6

P: n/a

Torsten Wiebesiek wrote:
Stefan Nwe wrote:
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;
}

Dec 23 '05 #7

P: n/a
On Thu, 22 Dec 2005 16:09:41 +0100, Torsten Wiebesiek
<ju***********@gmx.net> wrote in comp.lang.c++:
Stefan Nwe wrote:
Matthias Matker wrote:
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)


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.
Be aware of the difference between 'declaration' and 'definition'.

Torsten


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 24 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.