already defined problem with header files

B

Bruintje Beer

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
 
O

Ondra Holub

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

Victor Bazarov

Bruintje said:
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
 
R

red floyd

Bruintje said:
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.
 
P

Philipp Reh

Bruintje Beer napsal:

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.
 
V

Victor Bazarov

Philipp said:
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
 
B

Bruintje Beer

Victor Bazarov said:
Bruintje said:
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
Hi,

The use of const char f1[] = solved the problem. Thanks a lot;

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top