Scope resolution in C++

This is a discussion on Scope resolution in C++ within the C++ Programming forums, part of the General Programming Boards category; i have the following class Code: #ifndef CLRSCR_H #define CLRSCR_H #include <windows.h> namespace clrscr { void clear_screen ( void ) ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Scope resolution in C++

    i have the following class

    Code:
    #ifndef CLRSCR_H
    #define CLRSCR_H
    
    #include <windows.h>
    
    namespace clrscr
    {
    	void clear_screen ( void )
    	{
    	  DWORD n;                         /* Number of characters written */
    	  DWORD size;                      /* number of visible characters */
    	  COORD coord = {0};               /* Top left screen position */
    	  CONSOLE_SCREEN_BUFFER_INFO csbi;
    
    	  /* Get a handle to the console */
    	  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    
    	  GetConsoleScreenBufferInfo ( h, &csbi );
    
    	  /* Find the number of characters to overwrite */
    	  size = csbi.dwSize.X * csbi.dwSize.Y;
    
    	  /* Overwrite the screen buffer with whitespace */
    	  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
    	  GetConsoleScreenBufferInfo ( h, &csbi );
    	  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
    
    	  /* Reset the cursor to the top left position */
    	  SetConsoleCursorPosition ( h, coord );
    	}
    }
    
    #endif
    If i include this class in two .cpp files, i get a link error

    Code:
    error LNK2005: "void __cdecl clrscr::clear_screen(void)" (?clear_screen@clrscr@@YAXXZ) already defined in .... .obj
    I thought this only happens when you try include same header in two different .h files, not .cpp ...?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That rule of thumb only applies if you do not lie about your headers. Since you have the body of a function, this is not a header.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,113
    That said, you probably would place the function definition in the header if the function was meant to be inline. In this case clear_screen is probably not a good candidate for an inline function, but if you did declare the function inline there should not be a linker error even if the compiler chooses not to inline the function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    When you put #include in your program, the compiler will do a copy and paste of the file's content into the source file before doing the compilation. Since you have #include in both of your source files, you get two copies of the header file's contents - one in each source file. Also, since the header file contains actual code and not just a function prototype (which I'm guessing is what the header should probably just be) you have the code for the clear_screen function being compiled twice - once into each object file. When the linker tries to combine the two object files into a single executable, it sees this duplicate code and throws the above error at you complaining that you essentially have two of the same thing - the final executable has code that calls the clear_screen function (presumably) but does not know which "version" to call which is why this is an error.

    You need to make your header file just a simple function prototype (minus the actual code). The code should go in it's own source file and compiled along with the other two source files.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    thanx all ... BTW, is that function clear_screen a lot more better than system("cls") in terms of portability? i know its not perfect either but can one get away with it .. I wanted to use pdcurses from GNU but getting errors ...

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    BTW, is that function clear_screen a lot more better than system("cls") in terms of portability?
    Well, both work only in Windows, so it's at least equal. The advantage is that you can replace the implementation with one that works on other systems, which isn't really possible with system("cls").

    By the way, why a namespace for a single function?
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,113
    Quote Originally Posted by csonx_p
    BTW, is that function clear_screen a lot more better than system("cls") in terms of portability? i know its not perfect either but can one get away with it
    I think we already answered you on the second page of your thread press any key to continue.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. quick question about scope resolution
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 11-03-2002, 10:04 PM

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