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
    23,623
    Unfortunately, you can initialise member arrays like that. You should assign the values "manually".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,054
    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 10: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, 03:01 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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