Thread: Grrr! So so easy.....

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Grrr! So so easy.....

    i have a header file named calg.h which i hoped would contain a global name space, but no matter what i try, i keep getting redefiniton errors, either at compile time, or at linker time.

    Code:
    //calg.h
    #ifndef CALG_H
    #define CALG_H
    
    namespace alg
    {
         //this could easily change and could be used throughout the entire program
         char possible_vars[] = "abcdefghijklmnopqrstuvwxyz"
    }
    
    #endif
    main.cpp include complex.h which includes calg.h. main.cpp then include term.h which in turn include calg.h. i get the linker error that alg:ossible_vars is already defined in complex.obj.

    i know that defining the variable in the header is not good (to the extent of which, i don't know)..but i tried creating a calg.cpp and .............................

    i just had a thought - i didn't make it const ............be right back .................. ok, i placed a const before the definition/declaration of possible_vars and it compiled (or linked rather) just fine.

    what gives?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Probably because you have code / initialised data inside the header file.
    This has always been a no-no, it's nothing to do with namespaces.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    In the header, just declare the variable with the extern keyword.
    Then initialize the variable in one source file.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    hmmm.....
    Code:
    //aaa.h
    extern "C" char a[];
    
    namespace alg
    {
    	char a[];
    }
    Code:
    //aaa.cpp
    #include "aaa.h"
    char alg::a[] = "abcd";

    seems like i should have to use: extern "C" char alg::a[];

    nonetheless, thanks a lot Sang
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 05:28 AM
  3. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM