I have a h file with this code:

#include <windows.h>
#include <iostream>

struct Color
{
    int color;

    Color(int color_): color(color_) {}

    Color operator + (const Color & other) const { return Color(this->color | other.color); }
};

#define FORE_LIGHT(color) const Color cfl##color##_ = FOREGROUND_##color | FOREGROUND_INTENSITY;
#define BACK_LIGHT(color) const Color cbl##color##_ = BACKGROUND_##color | BACKGROUND_INTENSITY;
#define FORE_DARK(color)  const Color cfd##color##_ = FOREGROUND_##color;
#define BACK_DARK(color)  const Color cbd##color##_ = BACKGROUND_##color;

FORE_LIGHT(RED) FORE_LIGHT(GREEN) FORE_LIGHT(BLUE)
BACK_LIGHT(RED) BACK_LIGHT(GREEN) BACK_LIGHT(BLUE)
FORE_DARK (RED) FORE_DARK (GREEN) FORE_DARK (BLUE)
BACK_DARK (RED) BACK_DARK (GREEN) BACK_DARK (BLUE)

const Color cdefault_ = cfdRED_ + cfdGREEN_ + cfdBLUE_;

std::ostream & operator << (std::ostream & os, Color c)
{
    return SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c.color), os;
}

When I include this h file to another cpp file, no problem but if I include this to two files I have error multiply symbols found. Can somebody help me how to solve it?

In the header file, try putting code gards around it like this:

#ifmdef MYHEADERFILE
#define MYHEDERFILE
// your code goes here

#endif

Edited 3 Years Ago by Ancient Dragon

Now I have these errors:

Error 2 error C2143: syntax error : missing ';' before '.' c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h 13112
Error 6 error C2143: syntax error : missing ';' before '.' c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h 13112
Error 3 error C2238: unexpected token(s) preceding ';' c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h 13112
Error 7 error C2238: unexpected token(s) preceding ';' c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h 13112
9 IntelliSense: expected a ';' c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h 13112

repost your header file

There should NOT be a semicolon at the end of #define statements -- recheck the defines in your header file to make sure semicolons do not exist. lines 13-16 contain semicolons, which is wrong.

Edited 3 Years Ago by Ancient Dragon

Since you're programming for windows, if you use VC++ and the CLR for .net, you can use the console class and it's built in methods for changing the foreground and background of the text. There's a pretty good sample here. The code also shows how to use an enumerator to get all the possible colors.

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.