473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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
Jan 4 '07 #1
7 3267
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
Jan 4 '07 #2

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

Jan 4 '07 #3
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
Jan 4 '07 #4
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.
Jan 4 '07 #5
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.
Jan 4 '07 #6
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
Jan 4 '07 #7

"Victor Bazarov" <v.********@comAcast.netschreef in bericht
news:en**********@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
Jan 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dariusz | last post by:
I have a problem where when I run the PHP code offline, there are no errors produced and the code runs as expected. However when I uploaded the same script and run it, it says the headers have...
1
by: None | last post by:
Hello, I am a total newbie to PHP and programming in general. I am playing around with a PHP / MySQL shopping cart script which I found at...
4
by: Q | last post by:
Hello, I have composed the following simple php file: <html> <head><title> Title. </title></head> <body> <?php header("Location: http://www.something.com/tmp2.php"); ?> </body>
3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
2
by: nospam_timur | last post by:
I'm writing a Linux device driver that needs to compile with several different Linux versions. In my code, I need to reference certain functions by their address alone. Something like this: ...
1
by: Angus Comber | last post by:
Hello I have a function which I have defined in a .h file called LogError. I use a sort of global .h file which is included in all cpp files - StdAfx.h - this file #include's my .h file with...
3
by: Rohini | last post by:
Hi , I am getting the following LINK 2005 error when I tried to build my project in vc++7.1. I am doing the build process in win32release mode. I have searched google on this but whatever I...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
9
by: upendrajpr | last post by:
Hi everybody, I am working with site development with PHP. My problem is I have made a header file of header.inc . I use php function for session start but I when I to to php file for some...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.