473,385 Members | 1,642 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,385 software developers and data experts.

(simple) header file problem

Hi All,

I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif

----------- file other.h -------------
#ifndef other_
#define other_

#include "Constants.h"

class Other {
public:
void crap();
};

#endif

----------- file other.cpp ----------
#include "Other.h"

void Other::crap(){
//stuff to do stuff
}
---------- file crap.cpp -----------
#include <iostream>
#include "constants.h"
#include "other.h"

using namespace std;

void doStuff(char* arg){
cout << "arg was " << arg << endl;
}

int main(int argc, char* argv[]){
if(argc < ARGCOUNT)
doStuff(NAME);
else
doStuff(argv[1]);
}
------------------------------------

When I compile, the compiler complains that I have 'multiple definition of
_NAME'
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?

It seems to be something to do with the char*, but I can't figure out what
or why.....

Thanks for your help

Michael
Apr 11 '07 #1
10 1681
michael wrote:
Hi All,

I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif
When I compile, the compiler complains that I have 'multiple definition of
_NAME'
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?
The others are constants, why isn't NAME?

--
Ian Collins.
Apr 11 '07 #2
Don't you have one of cpp file as part as the other one? I ain't
expert but I suppose that the multiple definition of _NAME is because
the compiler gets that definition more times. It could be due the
including the header file "constants.h" from both (other.cpp,
crap.cpp) files...

michael napsal:
Hi All,

I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif

----------- file other.h -------------
#ifndef other_
#define other_

#include "Constants.h"

class Other {
public:
void crap();
};

#endif

----------- file other.cpp ----------
#include "Other.h"

void Other::crap(){
//stuff to do stuff
}
---------- file crap.cpp -----------
#include <iostream>
#include "constants.h"
#include "other.h"

using namespace std;

void doStuff(char* arg){
cout << "arg was " << arg << endl;
}

int main(int argc, char* argv[]){
if(argc < ARGCOUNT)
doStuff(NAME);
else
doStuff(argv[1]);
}
------------------------------------

When I compile, the compiler complains that I have 'multiple definition of
_NAME'
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?

It seems to be something to do with the char*, but I can't figure out what
or why.....

Thanks for your help

Michael
Apr 11 '07 #3
MIUSS wrote:
Don't you have one of cpp file as part as the other one? I ain't
expert but I suppose that the multiple definition of _NAME is because
the compiler gets that definition more times. It could be due the
including the header file "constants.h" from both (other.cpp,
crap.cpp) files...
Please don't top post.
michael napsal:
>>Hi All,

I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif

----------- file other.h -------------
#ifndef other_
#define other_

#include "Constants.h"

class Other {
public:
void crap();
};

#endif

----------- file other.cpp ----------
#include "Other.h"

void Other::crap(){
//stuff to do stuff
}
---------- file crap.cpp -----------
#include <iostream>
#include "constants.h"
#include "other.h"

using namespace std;

void doStuff(char* arg){
cout << "arg was " << arg << endl;
}

int main(int argc, char* argv[]){
if(argc < ARGCOUNT)
doStuff(NAME);
else
doStuff(argv[1]);
}
------------------------------------

When I compile, the compiler complains that I have 'multiple definition of
_NAME'
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?

It seems to be something to do with the char*, but I can't figure out what
or why.....

Thanks for your help

Michael


--
Ian Collins.
Apr 11 '07 #4

MIUSS wrote:
Don't you have one of cpp file as part as the other one? I ain't
expert but I suppose that the multiple definition of _NAME is because
the compiler gets that definition more times. It could be due the
including the header file "constants.h" from both (other.cpp,
crap.cpp) files...

michael napsal:
Hi All,

I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif

----------- file other.h -------------
#ifndef other_
#define other_

#include "Constants.h"

class Other {
public:
void crap();
};

#endif

----------- file other.cpp ----------
#include "Other.h"

void Other::crap(){
//stuff to do stuff
}
---------- file crap.cpp -----------
#include <iostream>
#include "constants.h"
#include "other.h"

using namespace std;

void doStuff(char* arg){
cout << "arg was " << arg << endl;
}

int main(int argc, char* argv[]){
if(argc < ARGCOUNT)
doStuff(NAME);
else
doStuff(argv[1]);
}
------------------------------------

When I compile, the compiler complains that I have 'multiple definition of
_NAME'
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?

It seems to be something to do with the char*, but I can't figure out what
or why.....

Thanks for your help

Michael
First : Not the compiler, rather linker will complaint for multiple
definitation for NAME. As at compile
multiple definitions you have avoided through #ifndef ..statements but
at link time these files migth be
included in other files so multiple definition found
SECOND: All other variables you have declared as const except name. In
c++ const has internal linkage so
const variables will not be detected by linker but NAME has external
linkage so linker may find multiple definition
of NAME (if included in multiple files)

Apr 11 '07 #5
In article <58**************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>michael wrote:
>I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif
When I compile, the compiler complains that I have 'multiple definition of
_NAME'
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?
The others are constants, why isn't NAME?
As well, if it doesn't need to be a pointer, [] it.

As to your original question: It would be multiply defined everywhere.
The consts in this case escape that because they are int's,
however, the pointer (or even in the [] case) will be laid
down every time you #include the constants.h file.
So you probably want to define it once (in a non-header file)
and extern it everywhere else (that is, in the header file).
There are some other macros you can establish to automate it
if used across many such objects, and how to do that is
probably in the FAQ (or the C FAQ).
--
Greg Comeau / 4.3.9 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Apr 12 '07 #6
In article <11**********************@d57g2000hsg.googlegroups .com>,
<ni**********@st.comwrote:
>MIUSS wrote:
>Don't you have one of cpp file as part as the other one? I ain't
expert but I suppose that the multiple definition of _NAME is because
the compiler gets that definition more times. It could be due the
including the header file "constants.h" from both (other.cpp,
crap.cpp) files...

michael napsal:
------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif

----------- file other.h -------------
#ifndef other_
#define other_

#include "Constants.h"

class Other {
public:
void crap();
};

#endif

----------- file other.cpp ----------
#include "Other.h"

void Other::crap(){
//stuff to do stuff
}
---------- file crap.cpp -----------
#include <iostream>
#include "constants.h"
#include "other.h"

using namespace std;

void doStuff(char* arg){
cout << "arg was " << arg << endl;
}

int main(int argc, char* argv[]){
if(argc < ARGCOUNT)
doStuff(NAME);
else
doStuff(argv[1]);
}
------------------------------------

When I compile, the compiler complains that I have 'multiple definition of
_NAME'
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?

It seems to be something to do with the char*, but I can't figure out what
or why.....

Thanks for your help

Michael

First : Not the compiler, rather linker will complaint for multiple
definitation for NAME. As at compile
multiple definitions you have avoided through #ifndef ..statements but
at link time these files migth be
included in other files so multiple definition found
SECOND: All other variables you have declared as const except name. In
c++ const has internal linkage so
const variables will not be detected by linker but NAME has external
linkage so linker may find multiple definition
of NAME (if included in multiple files)
True, but generally, laying down a copy of NAME, and perhaps
the literal too, in every translation unit is probably not the best
solution.
--
Greg Comeau / 4.3.9 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Apr 12 '07 #7

"Ian Collins" <ia******@hotmail.comwrote in message
news:58**************@mid.individual.net...
michael wrote:
Hi All,

I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif
When I compile, the compiler complains that I have 'multiple definition
of
_NAME'
It has no problem with the other variables I have defined in
Constants.h, so
why the problem with NAME?
The others are constants, why isn't NAME?
I originally had is as a const, but I got other errors and while trying to
work them out I changed it to just char*

What I'm trying to achieve is simple use of the command line arguments, with
a default if no argument is entered:

const char* DEFAULT_FILENAME = "somefilename";
const int ARGS = 2;

int main(int argc, char* argv[]){
if(argc < ARGS)
doStuff(DEFAULT_FILENAME);
//doStuff("somefilename");
else
doStuff(argv[1]);
}
void doStuff(char* name){
}

If I do it like above I get 'no matching function call doStuff(const char*)
near match is doStuff(char*) ' errors.
Surely I dont need to have a separate function?
--
Ian Collins.

Apr 12 '07 #8
michael wrote:
"Ian Collins" <ia******@hotmail.comwrote
>>
The others are constants, why isn't NAME?

I originally had is as a const, but I got other errors and while trying to
work them out I changed it to just char*

What I'm trying to achieve is simple use of the command line arguments, with
a default if no argument is entered:

const char* DEFAULT_FILENAME = "somefilename";
Make this const char* const DEFAULT_FILENAME = "somefilename";

Note the second const, this declares the pointer to be a constant. This
will remove your duplicate definition problem. I'd avoid all caps here
as they make the name look like a macro.
const int ARGS = 2;

int main(int argc, char* argv[]){
if(argc < ARGS)
doStuff(DEFAULT_FILENAME);
//doStuff("somefilename");
else
doStuff(argv[1]);
}
void doStuff(char* name){
Make this void doStuff( const char* name) {

If your default is a string literal, you can't modify the value passed,
so the parameter should be const char*.

--
Ian Collins.
Apr 12 '07 #9
Ian Collins wrote:
michael wrote:
>>"Ian Collins" <ia******@hotmail.comwrote
>>>The others are constants, why isn't NAME?

I originally had is as a const, but I got other errors and while trying to
work them out I changed it to just char*

What I'm trying to achieve is simple use of the command line arguments, with
a default if no argument is entered:

const char* DEFAULT_FILENAME = "somefilename";


Make this const char* const DEFAULT_FILENAME = "somefilename";

Note the second const, this declares the pointer to be a constant. This
will remove your duplicate definition problem.
I should have added the caveat that this will add the literal to each
object file, so a better approach is to use extern and define the
constant in one source file.

--
Ian Collins.
Apr 12 '07 #10

"Ian Collins" <ia******@hotmail.comwrote in message
news:58*************@mid.individual.net...
michael wrote:
"Ian Collins" <ia******@hotmail.comwrote
>
The others are constants, why isn't NAME?
I originally had is as a const, but I got other errors and while trying
to
work them out I changed it to just char*

What I'm trying to achieve is simple use of the command line arguments,
with
a default if no argument is entered:

const char* DEFAULT_FILENAME = "somefilename";

Make this const char* const DEFAULT_FILENAME = "somefilename";

Note the second const, this declares the pointer to be a constant. This
will remove your duplicate definition problem. I'd avoid all caps here
as they make the name look like a macro.
const int ARGS = 2;

int main(int argc, char* argv[]){
if(argc < ARGS)
doStuff(DEFAULT_FILENAME);
//doStuff("somefilename");
else
doStuff(argv[1]);
}
void doStuff(char* name){

Make this void doStuff( const char* name) {

If your default is a string literal, you can't modify the value passed,
so the parameter should be const char*.

--
Ian Collins.
Thanks for that. Now I better go and read up on the use of const so I
understand what I'm doing :-)
Apr 13 '07 #11

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

Similar topics

13
by: Mtk | last post by:
Hi! Why does the following, simple, example produce such errors? I know it has to do with the two header files including each other and (moreover) the usage of the classes One and Two in the...
44
by: Neil Cerutti | last post by:
In Rob Pike's style guide he urges the following: Simple rule: include files should never include include files. If instead they state (in comments or implicitly) what files they need...
4
by: Andrew Ward | last post by:
Hi All, I was wondering if it is possible to use precompiled headers without having to include a <stdafx.h> or whatever in every source file. My problem is that I have a project that makes heavy...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
5
by: Sheldon Glickler | last post by:
Here is the problem I have. I have added a management interface in php for a website that was all html and flash. Whereas before items were hard-coded, now they are pulled up from a database. I...
4
by: asterixgallier | last post by:
Hello at all, i've got the following problem: - I define a class template in a header file - I implement the methods in a cpp file - I want to build an instance of my class template this...
6
by: sandy | last post by:
I think I just need a pair of eyes here... I can't see what I am doing wrong. I am creating a new Class for an assignment, Class File. I have a header and a cpp file. When I try to write the...
0
by: Grzegorz Smith | last post by:
Hi All. I 'm learning ZSI to use SOAP and I desperately need help. I'm working on example from tutorial -(examples/server/send_response/ simple/wsdl/). Here are my wsdl files...
2
by: liorjj | last post by:
Hi, I'm new in this area so just a very simple question, I have declared typedef in the header file says (header.h), and some functions in this header as well; My problem is that when i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: 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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.