Error: non constant expression

This is a discussion on Error: non constant expression within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am trying to do the following in C++ [code] static const int data Code: ={status_1 : 32, ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    10

    Exclamation Error: non constant expression

    Hi all,

    I am trying to do the following in C++

    [code]
    static const int data
    Code:
    ={status_1 : 32,
    status_2 : 192,
    status_3 : 35
    };
    
    typedef enum int [data[status_1]-1:0] {
    enum_value = 2,
    enum_value = 4
    };
    When i try to compile the above code in C++, I get the error as:
    Error: Non constant expression
    The following expression should be a constant.
    Expression: data[status_1]

    How can I access the value '32' from above array??

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    Sorry i missed one part in it.. Here is the complete code:
    Code:
    typedef enum {
    status_1 = 'h12,
    status_2 = 'h14,
    status_3 = 'h15
    } instruction;
    
    static const int data[instruction] ={status_1 : 32,
    status_2 : 192,
    status_3 : 35
    };
    
    typedef enum int [data[status_1]-1:0] {
    enum_value = 2,
    enum_value = 4
    };

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    Gosh, this looks like so much jibberish. Are these supposed to be bitfields?
    Code:
    static const int data
    ={status_1 : 32,
    status_2 : 192,
    status_3 : 35
    };
    That's what they look like. Unfortunately only struct members can be bitfields. Nothing you've written is like an array, either.

    What are you trying to do in English?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    i completed the code in the second post.

    Code:
    static const int data[instruction] ={status_1 : 32, 
    status_2 : 192, 
    status_3 : 35 
    };

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    Mhmmm ok.

    If you just want that to compile you would have to make 'instruction' a constant expression. Try declaring it as a constant integer and assigning a value, then use 'instruction' to make your fixed array. Arrays cannot be used as bitfields. In the initialization list, the expressions used have to be ints, so you could probably use status_1 etc, or 32, but not both. And not with a colon, that does not mean what you think it does.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    You can't have a bitfield of length 192, and probably not even 35 either.

    As you have been asked before, please explain what you want to do in English. We can't even see what you are trying to do from this meaningless gibberish uncompileable code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. illegal constant expression
    By nta in forum C Programming
    Replies: 3
    Last Post: 03-13-2008, 11:35 AM
  2. Does every expression results into constant value?
    By forumuser in forum C Programming
    Replies: 12
    Last Post: 04-18-2007, 08:08 AM
  3. constant integeral expression?
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 09-11-2006, 04:55 AM
  4. expected constant expression
    By Brian in forum C Programming
    Replies: 4
    Last Post: 12-03-2003, 02:30 PM

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