const char VS global char

This is a discussion on const char VS global char within the C++ Programming forums, part of the General Programming Boards category; I apologize if this appears twice. The first time I tried to post my computer froze and I had to ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    const char VS global char

    I apologize if this appears twice. The first time I tried to post my computer froze and I had to reboot. I didn't see it posted anywhere so I am trying to post it again.



    I have the following code:
    Code:
    //******************************************
    //CONSTANT
    const char ERROR1 []= "You have an invalid selection";
    
    //******************************************
    //CLASS DECLARATION
    
    class ArrayException : public exception
    {
      public:
        char * what();
        ArrayException(char[]);
    
      private:
        char *message;
    
    };
    //*****************************************
    //CONSTRUCTOR
    ArrayException::ArrayException(char errorMessage [])
    {
    
        message = errorMessage; 
    
    }
    In my main I am calling

    ArrayException(ERROR1)


    My compiler is giving me the following error;


    error C2440: 'initializing' : cannot convert from 'char [44]' to 'const char *[]


    If I change my constant variable to a global variable everything works just fine. I would like to use the constant variable if possible.

    Any sugestions?

    Thanks,

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Make message const?

    EDIT:
    I think the ArrayException constructor should accept a const char * too.

    EDIT++:
    Forget what I've said. I'm confused.
    Last edited by Dante Shamest; 09-04-2003 at 12:17 PM.

  3. #3
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Code:
    ArrayException::ArrayException(char errorMessage [])
    should be

    Code:
    ArrayException::ArrayException(const char *errorMessage)
    You are trying to pass a constant variable to a function can change your constant. The compiler wont let it happen.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 10:33 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Playing card data structure?
    By crypticgeek in forum C++ Programming
    Replies: 10
    Last Post: 12-31-2006, 05:29 PM
  5. Replies: 7
    Last Post: 06-16-2006, 10:23 PM

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