| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Bruintje Beer" |
| Date: |
04 Jan 2007 01:33:16 PM |
| Object: |
already defined problem with header files |
Hello,
I have an include file constants.h see below. Later I want to include this
header file in two c++ source files because they both need the variables f1,
f2 and f3. At link time I got an error that f1, f2 and f3 are alread defined
in one of the c++ source files. How can I change my code so both c++ source
files can have access to the fields f1, f2 and f3.
John
#ifndef __CONSTANTS_H
#define __CONSTANTS_H
namespace demo
{
const char* f1 = "field_1";
const char* f2 = "field_2";
const char* f3 = "field_3";
}
#endif
.
|
|
| User: "Ondra Holub" |
|
| Title: Re: already defined problem with header files |
04 Jan 2007 02:03:05 PM |
|
|
Bruintje Beer napsal:
Hello,
I have an include file constants.h see below. Later I want to include this
header file in two c++ source files because they both need the variables f1,
f2 and f3. At link time I got an error that f1, f2 and f3 are alread defined
in one of the c++ source files. How can I change my code so both c++ source
files can have access to the fields f1, f2 and f3.
John
#ifndef __CONSTANTS_H
#define __CONSTANTS_H
namespace demo
{
const char* f1 = "field_1";
const char* f2 = "field_2";
const char* f3 = "field_3";
}
#endif
You need global variable.
1st possibility for C and C++:
In header file:
extern const char* f1;
In 1 of implementation files:
const char* f1 = "asfaasdfasd";
2nd possibility for C++:
In header file:
struct demo
{
static const char* f1;
static const char* f2;
static const char* f3;
};
In implementation file:
const char* demo::f1 = "field_1";
const char* demo::f2 = "field_2";
const char* demo::f3 = "field_3";
.
|
|
|
| User: "Philipp Reh" |
|
| Title: Re: already defined problem with header files |
04 Jan 2007 02:34:23 PM |
|
|
On Thu, 04 Jan 2007 12:03:05 -0800, Ondra Holub wrote:
Bruintje Beer napsal:
Hello,
I have an include file constants.h see below. Later I want to include this
header file in two c++ source files because they both need the variables f1,
f2 and f3. At link time I got an error that f1, f2 and f3 are alread defined
in one of the c++ source files. How can I change my code so both c++ source
files can have access to the fields f1, f2 and f3.
John
#ifndef __CONSTANTS_H
#define __CONSTANTS_H
namespace demo
{
const char* f1 = "field_1";
const char* f2 = "field_2";
const char* f3 = "field_3";
}
#endif
You need global variable.
1st possibility for C and C++:
In header file:
extern const char* f1;
In 1 of implementation files:
const char* f1 = "asfaasdfasd";
2nd possibility for C++:
In header file:
struct demo
{
static const char* f1;
static const char* f2;
static const char* f3;
};
In implementation file:
const char* demo::f1 = "field_1";
const char* demo::f2 = "field_2";
const char* demo::f3 = "field_3";
No, const implies internal linkage. You just have to make the pointers
themselves const.
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: already defined problem with header files |
04 Jan 2007 02:48:48 PM |
|
|
Philipp Reh wrote:
On Thu, 04 Jan 2007 12:03:05 -0800, Ondra Holub wrote:
[..]
2nd possibility for C++:
In header file:
struct demo
{
static const char* f1;
static const char* f2;
static const char* f3;
};
In implementation file:
const char* demo::f1 = "field_1";
const char* demo::f2 = "field_2";
const char* demo::f3 = "field_3";
No, const implies internal linkage. You just have to make the pointers
themselves const.
Static class members have external linkage (9.4.2/6). Notice the change
from 'namespace' to 'struct' in the suggestion.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|
|
|
| User: "red floyd" |
|
| Title: Re: already defined problem with header files |
04 Jan 2007 02:09:20 PM |
|
|
Bruintje Beer wrote:
Hello,
I have an include file constants.h see below. Later I want to include this
header file in two c++ source files because they both need the variables f1,
f2 and f3. At link time I got an error that f1, f2 and f3 are alread defined
in one of the c++ source files. How can I change my code so both c++ source
files can have access to the fields f1, f2 and f3.
John
#ifndef __CONSTANTS_H
#define __CONSTANTS_H
namespace demo
{
const char* f1 = "field_1";
const char* f2 = "field_2";
const char* f3 = "field_3";
}
#endif
Others have dealt with the main issue. However, your include guard is
illegal. Any identifier with two consecutive underscores is reserved to
the implementation. You aren't allowed to define it yourself.
And don't go thinking about just removing the first underscore -- Any
identifier with a leading underscore, followed by an upper case letter
is also reserved.
Try CONSTANTS_H_ instead.
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: already defined problem with header files |
04 Jan 2007 02:03:41 PM |
|
|
Bruintje Beer wrote:
I have an include file constants.h see below. Later I want to include
this header file in two c++ source files because they both need the
variables f1, f2 and f3. At link time I got an error that f1, f2 and
f3 are alread defined in one of the c++ source files. How can I
change my code so both c++ source files can have access to the fields
f1, f2 and f3.
John
#ifndef __CONSTANTS_H
#define __CONSTANTS_H
namespace demo
{
const char* f1 = "field_1";
const char* f2 = "field_2";
const char* f3 = "field_3";
}
#endif
Generally speaking, you probably want to make the pointers constant
as well:
const char* const f1 = ...
Try it. If that doesn't work, define them as arrays:
const char f1[] = ...
If *that* doesn't work, you can always declare them as extern and
define them in only one of your C++ files:
extern const char* const f1;
// in one of your C++ files:
extern const char* const f1 = "field_1";
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
| User: "Bruintje Beer" |
|
| Title: Re: already defined problem with header files |
04 Jan 2007 03:15:43 PM |
|
|
"Victor Bazarov" <v.Abazarov@comAcast.net> schreef in bericht
news:enjmiu$9mu$1@news.datemas.de...
Bruintje Beer wrote:
I have an include file constants.h see below. Later I want to include
this header file in two c++ source files because they both need the
variables f1, f2 and f3. At link time I got an error that f1, f2 and
f3 are alread defined in one of the c++ source files. How can I
change my code so both c++ source files can have access to the fields
f1, f2 and f3.
John
#ifndef __CONSTANTS_H
#define __CONSTANTS_H
namespace demo
{
const char* f1 = "field_1";
const char* f2 = "field_2";
const char* f3 = "field_3";
}
#endif
Generally speaking, you probably want to make the pointers constant
as well:
const char* const f1 = ...
Try it. If that doesn't work, define them as arrays:
const char f1[] = ...
If *that* doesn't work, you can always declare them as extern and
define them in only one of your C++ files:
extern const char* const f1;
// in one of your C++ files:
extern const char* const f1 = "field_1";
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Hi,
The use of const char f1[] = solved the problem. Thanks a lot;
John
.
|
|
|
|
|
| User: "" |
|
| Title: Re: already defined problem with header files |
04 Jan 2007 01:59:41 PM |
|
|
try
const char f1[] = "field_1";
Bruintje Beer wrote:
Hello,
I have an include file constants.h see below. Later I want to include this
header file in two c++ source files because they both need the variables f1,
f2 and f3. At link time I got an error that f1, f2 and f3 are alread defined
in one of the c++ source files. How can I change my code so both c++ source
files can have access to the fields f1, f2 and f3.
John
#ifndef __CONSTANTS_H
#define __CONSTANTS_H
namespace demo
{
const char* f1 = "field_1";
const char* f2 = "field_2";
const char* f3 = "field_3";
}
#endif
.
|
|
|
|

|
Related Articles |
|
|