Thread: Scope resolution in C++

  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,336
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,817
    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,895
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08:18 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10: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