先前声明的C ++函数

时间:2011-04-10 15:53:26

标签: c++ compilation redeclare

我正在为一个课程作品制作一个战舰计划,我正在调试播放器的部署我遇到了一个错误,我不能或找到一个解决方案:

bb.cpp:12: error: previous declaration of ‘int vert(int*, std::string)’

我在我的代码中搜索过以前对int vert的引用但找不到任何内容。

这是函数原型

//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();  

这是函数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;
}

我知道整个代码的编译中存在其他错误。

4 个答案:

答案 0 :(得分:2)

int vert(*shipx, shiptype) { .. }没有告诉参数的类型。

您忽略了告诉我们完整错误(跨越多行),但我怀疑它说前一个声明定义不匹配。

写:

int vert(int* shipx, string shiptype) { .. }

答案 1 :(得分:1)

您需要在函数定义中包含参数的类型和名称:

int vert (int *shipx, string shiptype) {
...
}

您还应该使原型和定义之间的参数名称匹配。

答案 2 :(得分:0)

这篇文章并没有真正解决你引用的错误。但是我的代码还有一个问题需要指出,建议您也为它提供解决方案。

在C ++中,switch-case只能有整数常量,而不是字符串文字。

所以这是错误的:

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;
}

这不会编译。

我建议您定义一个枚举ShipType,如下:

enum ShipType
{
    cv,
    ca,
    dd,
    ss,
    ms
};

然后声明类型为shiptype的{​​{1}},以便您可以写下:

ShipType

答案 3 :(得分:0)

int vert(int *shipx, std::string shiptype)应该是第三个灰色框中的定义,更改hor

相关问题