Connecting Tech Pros Worldwide Help | Site Map

already defined problem with header files

  #1  
Old January 4th, 2007, 07:35 PM
Bruintje Beer
Guest
 
Posts: n/a
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


  #2  
Old January 4th, 2007, 08:05 PM
gpriv@axonx.com
Guest
 
Posts: n/a

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
  #3  
Old January 4th, 2007, 08:05 PM
Ondra Holub
Guest
 
Posts: n/a

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";

  #4  
Old January 4th, 2007, 08:15 PM
Victor Bazarov
Guest
 
Posts: n/a

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


  #5  
Old January 4th, 2007, 08:15 PM
red floyd
Guest
 
Posts: n/a

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.
  #6  
Old January 4th, 2007, 08:45 PM
Philipp Reh
Guest
 
Posts: n/a

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.
  #7  
Old January 4th, 2007, 08:55 PM
Victor Bazarov
Guest
 
Posts: n/a

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


  #8  
Old January 4th, 2007, 09:25 PM
Bruintje Beer
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with include header file chat answers 9 May 25th, 2006 05:35 PM
c+ header files pooja answers 3 September 26th, 2005 09:15 AM
Header Files and Interfaces Yet Again Steven T. Hatton answers 11 July 22nd, 2005 01:33 PM
How do header files work? matthurne answers 16 July 22nd, 2005 12:23 PM