473,474 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Create a 2d array of class location

5 New Member
Hi, I'm trying to create a 2d array of class location which has x and y members. The compiler keep giving me these 2 errors:

error C2059: syntax error : '{'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body

What is the correct syntax for such declaration?
This is what i have:

#include "stdafx.h"
#using <mscorlib.dll>
using namespace std;

const int r = 9, c = 9;

class location
{
public:
int x, y;
location(int x, int y)
{
x = x;
y = y;
}
};

class Board
{
public:
int board[r][c];

location CellIndex[r][c] =
{ { (0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)},
{ (0,3), (0,4), (0,5), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5)},
{ (0,6), (0,7), (0,8), (1,6), (1,7), (1,8), (2,6), (2,7), (2,8)},
{ (3,0), (3,1), (3,2), (4,0), (4,1), (4,2), (5,0), (5,1), (5,2)},
{ (3,3), (3,4), (3,5), (4,3), (4,4), (4,5), (5,3), (5,4), (5,5)},
{ (3,6), (3,7), (3,8), (4,6), (4,7), (4,8), (5,6), (5,7), (5,8)},
{ (6,0), (6,1), (6,2), (7,0), (7,1), (7,2), (8,0), (8,1), (8,2)},
{ (6,3), (6,4), (6,5), (7,3), (7,4), (7,5), (8,3), (8,4), (8,5)},
{ (6,6), (6,7), (6,8), (7,6), (7,7), (7,8), (8,6), (8,7), (8,8)}};
};

int _tmain()
{
return 0;
}
Dec 12 '06 #1
5 2956
horace1
1,510 Recognized Expert Top Contributor
in C++ you cannot initialise class data members when you declare them

if you can make CellIndex static you can initialise it so
Expand|Select|Wrap|Line Numbers
  1. class Board
  2. {
  3. public:
  4. int board[r][c];
  5. static location CellIndex[r][c];
  6. };
  7.  
  8. location Board::CellIndex[r][c] =
  9. {{location(0,0), location(0,1), location(0,2), location(1,0), location(1,1), location(1,2), location(2,0), location(2,1), location(2,2)},
  10. { location(0,3), location(0,4), location(0,5), location(1,3), location(1,4), location(1,5), location(2,3), location(2,4), location(2,5)},
  11. { location(0,6), location(0,7), location(0,8), location(1,6), location(1,7), location(1,8), location(2,6), location(2,7), location(2,8)},
  12. { location(3,0), location(3,1), location(3,2), location(4,0), location(4,1), location(4,2), location(5,0), location(5,1), location(5,2)},
  13. { location(3,3), location(3,4), location(3,5), location(4,3), location(4,4), location(4,5), location(5,3), location(5,4), location(5,5)},
  14. { location(3,6), location(3,7), location(3,8), location(4,6), location(4,7), location(4,8), location(5,6), location(5,7), location(5,8)},
  15. { location(6,0), location(6,1), location(6,2), location(7,0), location(7,1), location(7,2), location(8,0), location(8,1), location(8,2)},
  16. { location(6,3), location(6,4), location(6,5), location(7,3), location(7,4), location(7,5), location(8,3), location(8,4), location(8,5)},
  17. { location(6,6), location(6,7), location(6,8), location(7,6), location(7,7), location(7,8), location(8,6), location(8,7), location(8,8)}};
  18.  
  19.  
Dec 12 '06 #2
atxc212
5 New Member
I've moved this block

location CellIndex[r][c] =
{ { (0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)},
{ (0,3), (0,4), (0,5), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5)},
{ (0,6), (0,7), (0,8), (1,6), (1,7), (1,8), (2,6), (2,7), (2,8)},
{ (3,0), (3,1), (3,2), (4,0), (4,1), (4,2), (5,0), (5,1), (5,2)},
{ (3,3), (3,4), (3,5), (4,3), (4,4), (4,5), (5,3), (5,4), (5,5)},
{ (3,6), (3,7), (3,8), (4,6), (4,7), (4,8), (5,6), (5,7), (5,8)},
{ (6,0), (6,1), (6,2), (7,0), (7,1), (7,2), (8,0), (8,1), (8,2)},
{ (6,3), (6,4), (6,5), (7,3), (7,4), (7,5), (8,3), (8,4), (8,5)},
{ (6,6), (6,7), (6,8), (7,6), (7,7), (7,8), (8,6), (8,7), (8,8)}};
};

down inside main(), it complains 'initializing' : cannot convert from 'int' to 'location'. How do you initialize an array like this with elements that has multipult member values?
Dec 12 '06 #3
atxc212
5 New Member
Oops! Coming back from a break I noticed I forgot to give type to each elements in the last reply...been working for too long lol. Too bad I can't delete it, just pretend its not there >_<.

Anyway its working now, thanks for the help horace!
Dec 12 '06 #4
horace1
1,510 Recognized Expert Top Contributor
Oops! Coming back from a break I noticed I forgot to give type to each elements in the last reply...been working for too long lol. Too bad I can't delete it, just pretend its not there >_<.

Anyway its working now, thanks for the help horace!
alternativly you could create the CellIndex array when the constructor is called
Expand|Select|Wrap|Line Numbers
  1. const int r = 9, c = 9;
  2.  
  3. class location
  4. {
  5. public:
  6. int x, y;
  7. location() {};   // you need default constructor
  8. location(int x1, int y1)  // fixed parameters
  9. {
  10. x = x1;
  11. y = y1;
  12. }
  13. };
  14.  
  15. class Board
  16. {
  17. public:
  18. int board[r][c];
  19. location CellIndex[r][c];
  20.  
  21. Board()
  22. {
  23. location localCellIndex[r][c] =
  24. {{location(0,0), location(0,1), location(0,2), location(1,0), location(1,1), location(1,2), location(2,0), location(2,1), location(2,2)},
  25. { location(0,3), location(0,4), location(0,5), location(1,3), location(1,4), location(1,5), location(2,3), location(2,4), location(2,5)},
  26. { location(0,6), location(0,7), location(0,8), location(1,6), location(1,7), location(1,8), location(2,6), location(2,7), location(2,8)},
  27. { location(3,0), location(3,1), location(3,2), location(4,0), location(4,1), location(4,2), location(5,0), location(5,1), location(5,2)},
  28. { location(3,3), location(3,4), location(3,5), location(4,3), location(4,4), location(4,5), location(5,3), location(5,4), location(5,5)},
  29. { location(3,6), location(3,7), location(3,8), location(4,6), location(4,7), location(4,8), location(5,6), location(5,7), location(5,8)},
  30. { location(6,0), location(6,1), location(6,2), location(7,0), location(7,1), location(7,2), location(8,0), location(8,1), location(8,2)},
  31. { location(6,3), location(6,4), location(6,5), location(7,3), location(7,4), location(7,5), location(8,3), location(8,4), location(8,5)},
  32. { location(6,6), location(6,7), location(6,8), location(7,6), location(7,7), location(7,8), location(8,6), location(8,7), location(8,8)}};
  33.  // copy from localCellIndex to class data member CellIndex
  34.  // either use a loop
  35.  //   for (int i=0; i< r; i++)
  36.  //      for (int j=0; j<c; j++) CellIndex[i][j] = localCellIndex[i][j];
  37.  //
  38.  // or memcpy
  39.  memcpy(CellIndex, localCellIndex, r*c*sizeof(location));
  40. }
  41. };      
  42.  
  43.  
note class location needs a default constructor and the problem with the parameters of the constructor with two int parameters (you called the parameters the same names as the class data members)

be interesting to see if anyone knows how to use a constructor initialisation list to initialise CellIndex
Dec 12 '06 #5
atxc212
5 New Member
Again thanks for the input. I've played with it for a bit, I'm wondering is it possible to initialise the array with keyword new like this:

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #using <mscorlib.dll>
  4.  
  5. using namespace std;
  6.  
  7. const int r = 9, c = 9;
  8.  
  9. class location
  10. {
  11. public:
  12.     int x, y;
  13.     location(){};
  14.     location(int x, int y)
  15.     {
  16.         x = x;
  17.         y = y;    
  18.     }
  19. };
  20.  
  21.  
  22. class Board
  23. {
  24. public:
  25.     int board[r][c];
  26. };    
  27.  
  28.  
  29. void main()
  30. {        
  31.     location CellIndex[c][r] = new location[c][r] =
  32.             {    { new location(0,0),new location(0,1),new location(0,2),new location(1,0),new location(1,1),new location(1,2),new location(2,0),new location(2,1),new location(2,2)},
  33.                 { new location(0,3),new location(0,4),new location(0,5),new location(1,3),new location(1,4),new location(1,5),new location(2,3),new location(2,4),new location(2,5)},
  34.                 { new location(0,6),new location(0,7),new location(0,8),new location(1,6),new location(1,7),new location(1,8),new location(2,6),new location(2,7),new location(2,8)},
  35.                 { new location(3,0),new location(3,1),new location(3,2),new location(4,0),new location(4,1),new location(4,2),new location(5,0),new location(5,1),new location(5,2)},
  36.                 { new location(3,3),new location(3,4),new location(3,5),new location(4,3),new location(4,4),new location(4,5),new location(5,3),new location(5,4),new location(5,5)},
  37.                 { new location(3,6),new location(3,7),new location(3,8),new location(4,6),new location(4,7),new location(4,8),new location(5,6),new location(5,7),new location(5,8)},
  38.                 { new location(6,0),new location(6,1),new location(6,2),new location(7,0),new location(7,1),new location(7,2),new location(8,0),new location(8,1),new location(8,2)},
  39.                 { new location(6,3),new location(6,4),new location(6,5),new location(7,3),new location(7,4),new location(7,5),new location(8,3),new location(8,4),new location(8,5)},
  40.                 { new location(6,6),new location(6,7),new location(6,8),new location(7,6),new location(7,7),new location(7,8),new location(8,6),new location(8,7),new location(8,8)}};
Currently this gives me syntax errors : missing ';' before '{' on each line in the array initialization. If this is possible in C++, how should it be done and what is the difference with declaring with static in terms of functionality and memory usage.
Dec 12 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
4
by: mb | last post by:
what is the best way to do this: In a game I want to a class called "Items". This class will have the game items public class Items { public int Chair public int Table . . .and so on . . .
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
4
by: Miguel Dias Moura | last post by:
Hello, I created a datalist in an ASP.Net / VB page. I display the image and price of a few products. When a user clicks an image I want to load the page "detail.aspx?number=id" and send the...
9
by: MMesich | last post by:
I've got an order entry webservice whose WSDL looks like this: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.