C++ Constructors

B

bgordon0

Here's my class and its constructor:

class gBoard {
public:
gBoard();
private:
//cell[][] is like the 4th quadrant with Y first and always
positive
int cell[10][10]; //0 for empty, ship code for ship, -1 for hit
int hits[5]; //hits[shipnumber] is the number of times its been
hit
int setShips(int, int); //direction, ship#
void clearBoard();
void generateBoard();
string ships[5];
int shipSizes[5];
static char letters[10];
};

gBoard::gBoard() { //constructor
ships = {"destroyer", "submarine", "cruiser", "battleship",
"aircraft carrier"};
shipSizes = {2,3,3,4,5};
letters = {'A','B','C','D','E','F','G','H','I','J'};
clearBoard();
generateBoard();
}

I have those clearBoard() and generateBoard() functions below that
code, this is what they look like:

void gBoard::clearBoard() { ... }
void gBoard::generateBoard() { ... }

When I complile it tells me "xx.cpp:21: syntax error before `{' token".
This is the line of the ships array being filled with the name of the
ships. When I insert some dummy code above it, the line number in the
compiler error is still that array assignment line, so I know it's not
a problem with the line above. Also it tells me:

xx.cpp: At global scope:
xx.cpp:22: ISO C++ forbids declaration of `shipSizes' with no type
xx.cpp:22: initializer for scalar variable requires one element
xx.cpp:23: ISO C++ forbids declaration of `letters' with no type
xx.cpp:23: initializer for scalar variable requires one element
xx.cpp:24: ISO C++ forbids declaration of `clearBoard' with no type
xx.cpp:25: ISO C++ forbids declaration of `generateBoard' with no type
xx.cpp:26: syntax error before `}' token

These are already declared, and whats with the the error with }? That
line is the ending } on the constructor function. Could these weird
errors be related to the first one? What's interesting is that it
considers the body of the constructor as part of the main program.

Anyone know what's going on?

Thanks
 
J

Jim Langston

Here's my class and its constructor:

class gBoard {
public:
gBoard();
private:
//cell[][] is like the 4th quadrant with Y first and always
positive
int cell[10][10]; //0 for empty, ship code for ship, -1 for hit
int hits[5]; //hits[shipnumber] is the number of times its been
hit
int setShips(int, int); //direction, ship#
void clearBoard();
void generateBoard();
string ships[5];
int shipSizes[5];
static char letters[10];
};

gBoard::gBoard() { //constructor
ships = {"destroyer", "submarine", "cruiser", "battleship",
"aircraft carrier"};

This is not legal C++.
ships[0] = "destroyer";
ships[1] = "submarine";
//...
is legal C++ code.

Same with the following.
 
B

bgordon0

Oh I see, that syntax is only legal when initializing. I'll move the
variable initialization /and/ assignment down into the constructor. Is
that bad form?
Jim said:
Here's my class and its constructor:

class gBoard {
public:
gBoard();
private:
//cell[][] is like the 4th quadrant with Y first and always
positive
int cell[10][10]; //0 for empty, ship code for ship, -1 for hit
int hits[5]; //hits[shipnumber] is the number of times its been
hit
int setShips(int, int); //direction, ship#
void clearBoard();
void generateBoard();
string ships[5];
int shipSizes[5];
static char letters[10];
};

gBoard::gBoard() { //constructor
ships = {"destroyer", "submarine", "cruiser", "battleship",
"aircraft carrier"};

This is not legal C++.
ships[0] = "destroyer";
ships[1] = "submarine";
//...
is legal C++ code.

Same with the following.
shipSizes = {2,3,3,4,5};
letters = {'A','B','C','D','E','F','G','H','I','J'};
clearBoard();
generateBoard();
}

I have those clearBoard() and generateBoard() functions below that
code, this is what they look like:

void gBoard::clearBoard() { ... }
void gBoard::generateBoard() { ... }

When I complile it tells me "xx.cpp:21: syntax error before `{' token".
This is the line of the ships array being filled with the name of the
ships. When I insert some dummy code above it, the line number in the
compiler error is still that array assignment line, so I know it's not
a problem with the line above. Also it tells me:

xx.cpp: At global scope:
xx.cpp:22: ISO C++ forbids declaration of `shipSizes' with no type
xx.cpp:22: initializer for scalar variable requires one element
xx.cpp:23: ISO C++ forbids declaration of `letters' with no type
xx.cpp:23: initializer for scalar variable requires one element
xx.cpp:24: ISO C++ forbids declaration of `clearBoard' with no type
xx.cpp:25: ISO C++ forbids declaration of `generateBoard' with no type
xx.cpp:26: syntax error before `}' token

These are already declared, and whats with the the error with }? That
line is the ending } on the constructor function. Could these weird
errors be related to the first one? What's interesting is that it
considers the body of the constructor as part of the main program.

Anyone know what's going on?

Thanks
 
J

Jim Langston

Jim said:
Here's my class and its constructor:

class gBoard {
public:
gBoard();
private:
//cell[][] is like the 4th quadrant with Y first and always
positive
int cell[10][10]; //0 for empty, ship code for ship, -1 for hit
int hits[5]; //hits[shipnumber] is the number of times its been
hit
int setShips(int, int); //direction, ship#
void clearBoard();
void generateBoard();
string ships[5];
int shipSizes[5];
static char letters[10];
};

gBoard::gBoard() { //constructor
ships = {"destroyer", "submarine", "cruiser", "battleship",
"aircraft carrier"};

This is not legal C++.
ships[0] = "destroyer";
ships[1] = "submarine";
//...
is legal C++ code.

Same with the following.
shipSizes = {2,3,3,4,5};
letters = {'A','B','C','D','E','F','G','H','I','J'};
clearBoard();
generateBoard();
}

I have those clearBoard() and generateBoard() functions below that
code, this is what they look like:

void gBoard::clearBoard() { ... }
void gBoard::generateBoard() { ... }

When I complile it tells me "xx.cpp:21: syntax error before `{' token".
This is the line of the ships array being filled with the name of the
ships. When I insert some dummy code above it, the line number in the
compiler error is still that array assignment line, so I know it's not
a problem with the line above. Also it tells me:

xx.cpp: At global scope:
xx.cpp:22: ISO C++ forbids declaration of `shipSizes' with no type
xx.cpp:22: initializer for scalar variable requires one element
xx.cpp:23: ISO C++ forbids declaration of `letters' with no type
xx.cpp:23: initializer for scalar variable requires one element
xx.cpp:24: ISO C++ forbids declaration of `clearBoard' with no type
xx.cpp:25: ISO C++ forbids declaration of `generateBoard' with no type
xx.cpp:26: syntax error before `}' token

These are already declared, and whats with the the error with }? That
line is the ending } on the constructor function. Could these weird
errors be related to the first one? What's interesting is that it
considers the body of the constructor as part of the main program.

Anyone know what's going on?

Thanks

Oh I see, that syntax is only legal when initializing. I'll move the
variable initialization /and/ assignment down into the constructor. Is
that bad form?

This group uses bottom posting, not top posting. Message rearranged.

Initializing your class variables in your constructor initialization list is
good form.

Using a lot of arrays, however, is not always considered good form when an
stl container can be used in it's place easily. Such as std::vector.

If you are writing this from scratch, you might consider using std::vectors
instead. However, it is a pain in the neck to do 2d vectors (
std::vector<std::vector<int> > cell ) but can be done and in this class I
probably would.

You would use push_back to push items onto the vector, then access them the
same way you do with arrays. I.E.

std::vector<std::string> Ships;
Ships.push_back( "destroyer" );
Ships.push_back( "submarine" );
etc...
then you can use them like
Ships[0];
Ships[1];
etc...

Since you have a few of these refering to ships, why not make it it's own
struct though?

struct ShipData
{
std::string Name;
int Size;
int Hits;
}

Now you can do a vector, or even array if you want, of all this one one
place.

Now you can have the data encapsulated, and can make methods to set the
data. Since each ship needs a name and size, why not do this in the
constructor?

struct ShipData
{
std::string Name;
int Size;
int Hits;

ShipData( const std::string name = "", const int size = 0 ): Name( name),
Size( size ), Hits( 0 ) {}
}

But what all does this do for us?

std::vector<ShipData> Ships;
Ships.push_back( ShipData( "destroyer", 2 ) );
Ships.push_back( ShipData( "submarine", 3 ) );
Ships.push_back( ShipData( "cruiser", 3 ) );
Ships.push_back( ShipData( "battleship", 4 ) );
Ships.push_back( ShipData( "aircraft carrier", 5 ) );

std::cout << Ships[0].Name << std::endl;

Now your ships name ("destoryer") and size (2) are set in one space. You
don't have to look around for where they're set. And you only have to worry
about one array element instead of 3, etc...

The initial setup will be a little longer for you, because you have to
create this ship class/structure, but in the long run it'll be much easier
for you as you'll realize you may also want to know where a ship is hit, in
which spot, and you can just add it to the class. This is one thing I would
probably use an array for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top