CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Location
    England
    Posts
    68

    [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.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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); }
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Jan 2009
    Location
    England
    Posts
    68

    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
  •  





Click Here to Expand Forum to Full Width

Featured