Defining a namespace.

This is a discussion on Defining a namespace. within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem with defining a namespace and I know I'm going to shoot myself once I get it ...

  1. #1
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147

    Defining a namespace.

    I'm having a problem with defining a namespace and I know I'm going to shoot myself once I get it figured out.

    Anyway, I have the following header file with namespace defined:

    Code:
    #ifndef __LOGGER___
    #define __LOGGER___
    
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    namespace LogFile
    {
    	std::ofstream logFile;
    
    	void openLogFile(const char *filePath)
    	{
    		logFile.open("log.log");
    	}
    
    	void closeLogFile()
    	{
    		logFile.close();
    	}
    }
    
    #endif
    Fine and dandy.

    When I include the header file in more than one CPP file, I get linker errors (e.g., symbol has already been defined in ***.cpp).

    Clearly I'm not doing something right. I did a search on Google but I'm not finding anything relevant (most of the results revolve around the std namespace).

    I would appreciate any hints as to why I'm getting these linker errors. For an exact quote from the linker: "error LNK2005: "void __cdecl LogFile:penLogFile(const char*)" (?openLogFile@LogFile@@YAXPBD@Z) already defined in main.obj"
    - Leeor

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,360
    Excluding templates, 'inline' functions, and macros definitions should not be in a header file.

    Soma

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,904
    If you include that header in two files, there will be two openLogFile functions, so the linker complains.
    Namespaces aren't special in this. They merely define what you need to prefix to access the functions.
    So,
    either add "inline" to the functions to make sure the compiler and linker treats them as so, or,
    put the declarations in the header and the definition in a source file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    Thank you!

    I've never really bothered with namespaces but they seemed an effective choice for this particular application.

    Thanks again!
    - Leeor

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,904
    Oh, I love namespaces. They're always useful. Just remember that they aren't magical - the same rules that apply without them also apply with them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Namespace are all about names, and nothing else. They have no effect on duplication.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A side note, don't use the using directive in headers (since you can't "turn it off" for headers that are included after this one and might not expect std namespace to be visible) - particularly since you qualify the std::ofstream fully anyway.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21