Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 4th, 2007, 07:35 PM
Bruintje Beer
Guest
 
Posts: n/a
Default 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


  #2  
Old January 4th, 2007, 08:05 PM
gpriv@axonx.com
Guest
 
Posts: n/a
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles