C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-14-2009, 10:59 AM   #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' },
  { ' ' , ' ' , ' ' },
  { ' ' , ' ' , ' ' } };
}
FlyingShoes12 is offline   Reply With Quote
Old 11-14-2009, 11:03 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Unfortunately, you can initialise member arrays like that. You should assign the values "manually".
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-14-2009, 11:07 AM   #3
Registered User
 
Join Date: Apr 2006
Posts: 1,193
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.
King Mir is offline   Reply With Quote
Old 11-14-2009, 11:07 AM   #4
The larch
 
Join Date: May 2006
Posts: 3,082
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]);
}
__________________
I might be wrong.

Quote:
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).

Last edited by anon; 11-14-2009 at 11:11 AM.
anon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
what am I missing? (Program won't compile) steals10304 C Programming 3 08-25-2009 03:01 PM
Making C DLL using MSVC++ 2005 chico1st C Programming 26 05-28-2008 01:17 PM
load gif into program willc0de4food Windows Programming 14 01-11-2006 10:43 AM
Problem with Visual C++ Object-Oriented Programming Book. GameGenie C++ Programming 9 08-29-2005 11:21 PM
UNICODE and GET_STATE Registered C++ Programming 1 07-15-2002 03:23 PM


All times are GMT -6. The time now is 07:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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