Help with using header files

This is a discussion on Help with using header files within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a header file I found on this website I want to use to simplify using color. Code: ...

  1. #1
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50

    Can't get this color header to work

    Hi, I have a header file I found on this website I want to use to simplify using color.

    Code:
    /*
    *   Copyright (c) 2002
    *
    *   Made by Petter Strandmark ("Sang-drax")
    *   http://www.strandmark.com
    *   Needed for functionality : <windows.h>
    *
    */
    
    /*
    *  USAGE:
    *
    *  cout << RED << "This is red." << BLUE << "\nThis is blue.";
    *
    *
    */
    
    #ifndef COLOR_PETTER_H
    #define COLOR_PETTER_H
    
    
    //Retain ANSI/ISO Compability
    #ifdef WIN32
        #include <windows.h>
    #endif
    
    #include <iostream>
    
    
    namespace Petter
    {
        
        namespace
        {
            class Color
            {
                friend std::ostream& operator<<(std::ostream& stream,const Color& c);
    
            public:
                Color(unsigned short c): color(c) {}
                static bool IsAvailabe()
                {
                    #ifdef WIN32
                        return true;
                    #else
                        return false;
                    #endif   
                }
            //private:
                unsigned short color;
            };
    
            std::ostream& operator<<(std::ostream& stream,const Color& c)
            {
                stream.flush();
                #ifdef WIN32
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c.color);
                #endif
                stream.flush();
    
                return stream;
            }
    
            #ifdef WIN32
                const Color NORMAL  = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
                const Color WHITE   = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY;
                const Color RED     = FOREGROUND_RED|FOREGROUND_INTENSITY;
                const Color DKRED     = FOREGROUND_RED;
                const Color BLUE    = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY;
                const Color DKBLUE    = FOREGROUND_BLUE|FOREGROUND_GREEN;
                const Color GREEN   = FOREGROUND_GREEN|FOREGROUND_INTENSITY;
                const Color DKGREEN   = FOREGROUND_GREEN;
                const Color YELLOW  = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY;
                const Color BROWN   = FOREGROUND_RED|FOREGROUND_GREEN;
            #else
                const Color NORMAL  = 0;
                const Color WHITE   = 0;
                const Color RED     = 0;
                const Color DKRED     = 0;
                const Color BLUE    = 0;
                const Color DKBLUE    = 0;
                const Color GREEN   = 0;
                const Color DKGREEN   = 0;
                const Color YELLOW  = 0;
                const Color BROWN   = 0;
            #endif
        }
    
    }
    #endif //ifndef
    I'm using Dev-C++ 4.9.9.2, my question is, how do I go about turning this code into a header file? And how to I instalize it in my program?
    Last edited by Flakster; 09-21-2005 at 03:10 PM.

  2. #2
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    OK, I have it saved as a .h file in my include folder, to clarify that.

    I'm using just as it shows and I get an error.

    The compiler thinks that RED is a variable, which is what I was afraid of.

    Any idea how to get this header to work?

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    In your main code, at the top, add something like:
    Code:
    #include "colors.h"
    // then something like
    Petter::Color myColor;
    myColor.whatever
    I did see an extra namespace, I think, within the Petter namespace

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well I don't see what's hard about changing console text color. Just call this before outputting the text:
    Code:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
    //FOREGROUND_GREEN is just ONE of the many choices
    For more options and more details on this function go here:
    http://msdn.microsoft.com/library/de...tattribute.asp. That seems much easier, and quicker, than importing that header. That header is basically doing the exact same thing.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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