array initialization error

This is a discussion on array initialization error within the C++ Programming forums, part of the General Programming Boards category; I am writing a header file for a tic-tac-toe game. I am getting an error in my board initiailizaiton. please ...

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

    array initialization error

    I am writing a header file for a tic-tac-toe game. I am getting an error in my board initiailizaiton. please bear with me, I'm new at this.


    The error I'm getting is:
    Code:
    cannot convert ‘<brace-enclosed initializer list>’ to ‘char’ in assignment
    and here is the relevant code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class turn {
    
     public:
      turn();
      ~turn();
      void display();
      void getpick( char player );
      void change();
    
     private:
      char board[3][3];
      void valid();
    };
    
    turn::turn() {
     board[3][3] = {
      { 'a' , 'b' , 'c' },
      { ' ' , ' ' , ' ' },
      { ' ' , ' ' , ' ' } };
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    Unfortunately, you can initialise member arrays like that. You should assign the values "manually".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    You can't assign to an array. You have to use a loop.

    The brace syntax is only valid where you declare the array, and you cannot use it in objects. This will be fixed in C++0x.
    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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't assign arrays.

    Besides, you are even attempting to assign a whole array to a single element in the board array, which also happens to be out of bounds.

    With C++0x, you should be able to use the initialization list like this:

    Code:
    turn::turn():
      board 
      {
          { 'a' , 'b' , 'c' },
          { ' ' , ' ' , ' ' },
          { ' ' , ' ' , ' ' } 
      }
    {
    }
    With the current language standard, I'm afraid, you'll just have to set each character in the array separately (possible with the help of a loop). E.g:

    Code:
    turn::turn()
    {
        char board_init[3][3] = {
            { 'a' , 'b' , 'c' },
            { ' ' , ' ' , ' ' },
            { ' ' , ' ' , ' ' }
        };
        std::copy(&board_init[0][0], &board_init[3][3], &board[0][0]);
    }
    Last edited by anon; 11-14-2009 at 11:11 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 04:01 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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