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;
}
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 -
class Board
-
{
-
public:
-
int board[r][c];
-
static location CellIndex[r][c];
-
};
-
-
location Board::CellIndex[r][c] =
-
{{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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)}};
-
-
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?
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!
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 -
const int r = 9, c = 9;
-
-
class location
-
{
-
public:
-
int x, y;
-
location() {}; // you need default constructor
-
location(int x1, int y1) // fixed parameters
-
{
-
x = x1;
-
y = y1;
-
}
-
};
-
-
class Board
-
{
-
public:
-
int board[r][c];
-
location CellIndex[r][c];
-
-
Board()
-
{
-
location localCellIndex[r][c] =
-
{{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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)}};
-
// copy from localCellIndex to class data member CellIndex
-
// either use a loop
-
// for (int i=0; i< r; i++)
-
// for (int j=0; j<c; j++) CellIndex[i][j] = localCellIndex[i][j];
-
//
-
// or memcpy
-
memcpy(CellIndex, localCellIndex, r*c*sizeof(location));
-
}
-
};
-
-
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
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: - #include "stdafx.h"
-
-
#using <mscorlib.dll>
-
-
using namespace std;
-
-
const int r = 9, c = 9;
-
-
class location
-
{
-
public:
-
int x, y;
-
location(){};
-
location(int x, int y)
-
{
-
x = x;
-
y = y;
-
}
-
};
-
-
-
class Board
-
{
-
public:
-
int board[r][c];
-
};
-
-
-
void main()
-
{
-
location CellIndex[c][r] = new location[c][r] =
-
{ { 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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)},
-
{ 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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 . . .
|
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
...
|
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^>^...
| |
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...
|
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"...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |