|
-
January 24th, 2009, 07:11 AM
#1
[RESOLVED] Composition Issue
I am new to CPP programming and I am trying to create a class vmConsole that has another class vmColor embedded into it. I understand that because my console class "has a" color, I need to program my class like this. Once I have resolved this I wish to add another class vmCursor as a console program "has a" cursor. Here is the cut down version to show the issue:
Code:
#include <iostream>
#include <windows.h>
#define BLACK 0
#define WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
#define RED FOREGROUND_RED
#define GREEN FOREGROUND_GREEN
#define BLUE FOREGROUND_BLUE
#define YELLOW FOREGROUND_RED | FOREGROUND_GREEN
#define CYAN FOREGROUND_GREEN | FOREGROUND_BLUE
#define MAGENTA FOREGROUND_BLUE | FOREGROUND_RED
class vmColor
{
public:
vmColor(int fg = WHITE, int bg = BLACK);
void SetColor(int fg, int bg = BLACK);
private:
int m_nForegroundColor;
int m_nBackgroundColor;
};
class vmConsole
{
public:
void SetColor(int fg, int bg = BLACK) { cColor.SetColor(int fg, int bg); } // Error is here ?
private:
vmColor cColor;
};
vmColor::vmColor(int fg, int bg)
{
SetColor(fg, bg);
}
void vmColor::SetColor(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
wColor | FOREGROUND_INTENSITY);
m_nForegroundColor = ForgC;
m_nBackgroundColor = BackC;
return;
}
int main()
{
vmConsole cHcsConsole;
cHcsConsole.SetColor(RED);
std::cout << "Hello there!" << std::endl;
system("Pause");
return 0;
}
The compiler errors:
Code:
D:/Programming/Test/consoleTest.cpp: In member function `void vmConsole::SetColor(int, int)':
D:/Programming/Test/consoleTest.cpp:28: error: expected primary-expression before "int"
D:/Programming/Test/consoleTest.cpp:28: error: expected primary-expression before "int"
Execution terminated
Compilation Failed. Make returned 1
What the mind can conceive it can achieve.
-
January 24th, 2009, 08:59 AM
#2
Re: Composition Issue
You are specifying the types of the objects you are passing which is unnecessary and illegal. It should be written like this:
Code:
void SetColor(int fg, int bg = BLACK) { cColor.SetColor(fg, bg); }
-
January 24th, 2009, 09:13 AM
#3
Re: Composition Issue
Thanks Mybowlcut,
this works correctly now.
What the mind can conceive it can achieve.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|