Connecting Tech Pros Worldwide Help | Site Map

already defined problem with header files

Bruintje Beer
Guest
 
Posts: n/a
#1: Jan 4 '07
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


gpriv@axonx.com
Guest
 
Posts: n/a
#2: Jan 4 '07

re: already defined problem with header files


try

const char f1[] = "field_1";


Bruintje Beer wrote:
Quote:
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
Ondra Holub
Guest
 
Posts: n/a
#3: Jan 4 '07

re: already defined problem with header files



Bruintje Beer napsal:
Quote:
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";

Victor Bazarov
Guest
 
Posts: n/a
#4: Jan 4 '07

re: already defined problem with header files


Bruintje Beer wrote:
Quote:
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


red floyd
Guest
 
Posts: n/a
#5: Jan 4 '07

re: already defined problem with header files


Bruintje Beer wrote:
Quote:
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.
Philipp Reh
Guest
 
Posts: n/a
#6: Jan 4 '07

re: already defined problem with header files


On Thu, 04 Jan 2007 12:03:05 -0800, Ondra Holub wrote:
Quote:
Bruintje Beer napsal:
Quote:
>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.
Victor Bazarov
Guest
 
Posts: n/a
#7: Jan 4 '07

re: already defined problem with header files


Philipp Reh wrote:
Quote:
On Thu, 04 Jan 2007 12:03:05 -0800, Ondra Holub wrote:
[..]
Quote:
>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


Bruintje Beer
Guest
 
Posts: n/a
#8: Jan 4 '07

re: already defined problem with header files



"Victor Bazarov" <v.Abazarov@comAcast.netschreef in bericht
news:enjmiu$9mu$1@news.datemas.de...
Quote:
Bruintje Beer wrote:
Quote:
>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


Closed Thread