Array problem with const integral data

This is a discussion on Array problem with const integral data within the C++ Programming forums, part of the General Programming Boards category; const char Answer[20]=( 'B','D','A','A', 'C','A','B','A', 'C','D','B','C', 'D','A','D','C', 'C','B','D','A'); error C2864: 'TestGrader::Answer' : only static const integral data members can be ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Array problem with const integral data

    const char Answer[20]=( 'B','D','A','A',
    'C','A','B','A',
    'C','D','B','C',
    'D','A','D','C',
    'C','B','D','A');

    error C2864: 'TestGrader::Answer' : only static const integral data members can be initialized within a class


    I need to be able to use this data to grade someones answers to a survey

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Use the class constructor's initializer list.

    Initialization Lists in C++ - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    ok i have revised the code to
    Code:
    class TestGrader
    {
    public:
    	int setKey;
    	char grade[20];
    	char Answer[20] = {'B','D','A','A','C','A','B','A','C','D','B','C','B','A','D','C','C','B','D','A'};
    };
    
    
    
    	{	if(test.grade[test.setKey] = test.Answer[test.setKey])
    i get these three errors:

    error C2059: syntax error : '{'
    error C2334: unexpected token(s) preceding '{'; skipping apparent function body
    error C2039: 'Answer' : is not a member of 'TestGrader'

    any thoughts would be appreciated because i cannot figure out what is causing the errors due to the coding being correct

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You can't initialize the values of an array like that. You have to assign the values in the constructor. One option is to make an array in the source file and copy it using memcpy inside the constructor.
    Code:
    namespace
    {
      char Answer_Initializer[] = {'B','D','A','A','C','A','B','A','C','D','B','C','B','A','D','C','C','B','D','A'};
    }
    
    // ... later copy the contents into your Answers array in the constructor.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    You cannot use a member like that, initializer list or otherwise.

    What happened to Answers being const? And as long as it is const, they may as well be static.

    Being static allows you to initialise the array outside the class:
    Code:
    const char TestGrader::Answer[20] = 
        {'B','D','A','A','C','A','B','A','C','D','B','C','B','A','D','C','C','B','D','A'};
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    the reason that I don't understand how do make an array is because the teacher i had for C++ programming never explain anything

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    {	if(test.grade[test.setKey] = test.Answer[test.setKey])
    You sure your don't mean == instead of =?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Quote Originally Posted by JackValentine01 View Post
    the reason that I don't understand how do make an array is because the teacher i had for C++ programming never explain anything
    Look in the book recommendations sticky, for a good book.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

Tags for this Thread


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