Thread: Error

  1. #1
    Registered User Daxxl'n taurian's Avatar
    Join Date
    Sep 2013
    Posts
    2

    Error

    Asalam-o-alikum , i've just started out in C++ and i've been given an assignment related to the error correction.
    Earlier there were 32 errors in the program and now there are just 6. Em using DEV C++

    18 C:\Users`std:: operator<<' cannot appear in a constant-expression
    18 C:\Users `stream' was not declared in this scope
    18 C:\Users expected primary-expression before "const"
    18 C:\Users a function call cannot appear in a constant-expression
    18 C:\User ISO C++ forbids declaration of `type name' with no type
    18 C:\Users confused by earlier errors, bailing out
    these are the errors which i am unable to understand and correct them as em just a beginner kindly help me out.

    Code:
    #include<iostream>
    using namespace std;
    
    
    int main()
    {
        char RED ,BLUE;
        cout << RED << " This is Red." << endl << BLUE << " This is blue." << endl;
        cout << "Press enter to exit! ";
        cin.ignore();
        cin.get();
    }
    
    
        
    
    
            class Color
            {
                friend:operator<<( 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;
            };
     
            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
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > friend:operator<<( stream,const Color& c);
    This doesn't need a :

    > public;
    This ; should be a :
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should also compare the line 20 and 36... and see the difference
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User Daxxl'n taurian's Avatar
    Join Date
    Sep 2013
    Posts
    2

    reply

    Quote Originally Posted by Salem View Post
    > friendperator<<( stream,const Color& c);
    This doesn't need a :

    > public;
    This ; should be a :
    when i didn't used this the errors reached again to 32

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well without all the ifdef stuff, this compiles.
    Code:
    #include<iostream>
    using namespace std;
    
    
    int main()
    {
      char RED, BLUE;
      cout << RED << " This is Red." << endl << BLUE << " This is blue." << endl;
      cout << "Press enter to exit! ";
      cin.ignore();
      cin.get();
    }
    
    class Color {
      friend ostream & operator<<(ostream & stream, const Color & c);
    
    public:
      Color(unsigned short c):color(c) {
      }
      static bool IsAvailabe() {
        return false;
      }
      //private:
      unsigned short color;
    };
    
    ostream & operator<<(std::ostream & stream, const Color & c)
    {
      stream.flush();
      stream.flush();
    
      return stream;
    }
    
    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;
     //  useless brace here };
    First lesson is learn how to indent code.
    SourceForge.net: Indentation - cpwiki
    Then you don't end up in a horrible mess with mis-matched braces.

    Second, don't try and write several hundred lines of code at once, then panic over the flood of errors which make no sense.
    If the most error free lines you can usually write is 10 lines, then stick to writing at most 10 lines between compiles. When it does fail to compile, you know which 10 lines are the prime suspects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Third lesson: Ask questions the smart way.
    Error as a title is pretty much useless.
    Welcome to the forum.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  2. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM