(simple) header file problem

Discussion in 'C++' started by michael, Apr 11, 2007.

  1. michael

    michael Guest

    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
    michael, Apr 11, 2007
    #1
    1. Advertising

  2. michael

    Ian Collins Guest

    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.
    Ian Collins, Apr 11, 2007
    #2
    1. Advertising

  3. michael

    MIUSS Guest

    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
    MIUSS, Apr 11, 2007
    #3
  4. michael

    Ian Collins Guest

    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.
    Ian Collins, Apr 11, 2007
    #4
  5. michael

    Guest

    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, 2007
    #5
  6. michael

    Greg Comeau Guest

    In article <>,
    Ian Collins <> wrote:
    >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?
    Greg Comeau, Apr 12, 2007
    #6
  7. michael

    Greg Comeau Guest

    In article <>,
    <> wrote:
    >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?
    Greg Comeau, Apr 12, 2007
    #7
  8. michael

    michael Guest

    "Ian Collins" <> wrote in message
    news:...
    > 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.
    michael, Apr 12, 2007
    #8
  9. michael

    Ian Collins Guest

    michael wrote:
    > "Ian Collins" <> wrote
    >>
    >>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.
    Ian Collins, Apr 12, 2007
    #9
  10. michael

    Ian Collins Guest

    Ian Collins wrote:
    > michael wrote:
    >
    >>"Ian Collins" <> wrote
    >>
    >>>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.
    Ian Collins, Apr 12, 2007
    #10
  11. michael

    michael Guest

    "Ian Collins" <> wrote in message
    news:...
    > michael wrote:
    > > "Ian Collins" <> wrote
    > >>
    > >>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 :)
    michael, Apr 13, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    702
    lilburne
    Nov 2, 2003
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    603
    Jack Klein
    Jul 24, 2004
  3. kathy
    Replies:
    5
    Views:
    369
    mark zhong
    Feb 4, 2006
  4. Bit byte
    Replies:
    1
    Views:
    651
    benben
    Apr 19, 2006
  5. mlt
    Replies:
    2
    Views:
    832
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page