C++ function previously declared
I'm making a battleships program for a coursework piece and I was debugging the player's deployment I hit an error that I can't or find a solution to which is:
bb.cpp:12: error: previous declaration of ‘int vert(int*, std::string)’
I've searched through my code for any previous references to int vert but couldnt find anything.
Here is the function prototypes
//game function prototypes
int deploy();
int firing();
int gridupdte();
int win = 0;
int vert(int *x, string sa);
int hor(int *y, string s);
int check();
Here is function vert:
int开发者_开发技巧 vert(*shipx, shiptype) //Calculates where the bow of the ship should be (the pointy bit)
{
int shiplen;
int bow;
switch(shiptype) // Determines the length to add to the y co-ordinate
{
case "cv" : shiplen = 5; break;
case "ca" : shiplen = 4; break;
case "dd" : shiplen = 3; break;
case "ss" : shiplen = 3; break;
case "ms" : shiplen = 2; break;
}
bow = *shipx + shiplen;
*shipx = bow;
return *shipx;
}
int hor (*shipy, shiptype) //Calculates where the bow of the ship should be (the pointy bit)
{
int shiplen;
int bow;
switch(shiptype) // Determines the length to add to the x co-ordinate
{
case "cv" : shiplen = 5; break;
case "ca" : shiplen = 4; break;
case "dd" : shiplen = 3; break;
case "ss" : shiplen = 3; break;
case "ms" : shiplen = 2; break;
}
bow = *shipy + shiplen;
*shipy = bow;
return *shipy;
}
I am aware of other errors in the comipiling of the whole of the code.
int vert(*shipx, shiptype) { .. }
doesn't tell the types of the parameters.
You neglected to tell us the full error (which spans multiple lines), but I suspect it said that the previous declaration does not match the definition.
Write:
int vert(int* shipx, string shiptype) { .. }
You need to include the types and names of the parameters in a function definition:
int vert (int *shipx, string shiptype) {
...
}
You should also make the parameter names match between the prototypes and definitions.
int vert(int *shipx, std::string shiptype)
should be what the definition should look like in the third gray box, change hor
also
This post doesn't really address the error which you've quoted. But there is one more problem with your code which I want to point out, suggesting you a solution for it as well.
In C++, switch-case
can have only integral constant, not string literal.
So this is wrong:
switch(shiptype) // Determines the length to add to the x co-ordinate
{
case "cv" : shiplen = 5; break;
case "ca" : shiplen = 4; break;
case "dd" : shiplen = 3; break;
case "ss" : shiplen = 3; break;
case "ms" : shiplen = 2; break;
}
This wouldn't compile.
I would suggest you to define an enum ShipType
, as:
enum ShipType
{
cv,
ca,
dd,
ss,
ms
};
And then declare shiptype
of type ShipType
, so that you can write this:
switch(shiptype) // Determines the length to add to the x co-ordinate
{
case cv : shiplen = 5; break;
case ca : shiplen = 4; break;
case dd : shiplen = 3; break;
case ss : shiplen = 3; break;
case ms : shiplen = 2; break;
}
精彩评论