[英]C++ function previously declared
我正在為一個課件制作戰艦程序,並且正在調試玩家的部署,但遇到了我無法解決的錯誤或找到以下解決方案:
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();
這是功能版本:
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;
}
我知道在編寫整個代碼時存在其他錯誤。
int vert(*shipx, shiptype) { .. }
不告訴參數類型。
您忽略了告訴我們完整的錯誤(跨越多行),但是我懷疑它說先前的聲明與定義不匹配 。
寫:
int vert(int* shipx, string shiptype) { .. }
您需要在函數定義中包括參數的類型和名稱:
int vert (int *shipx, string shiptype) {
...
}
您還應該使原型和定義之間的參數名稱匹配。
這篇文章並沒有真正解決您引用的錯誤。 但我想指出的是您的代碼還有另一個問題,也建議您解決該問題。
在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
類型的ShipType
,以便您可以編寫以下代碼:
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;
}
int vert(int *shipx, std::string shiptype)
應該是第三個灰色框中定義的樣子,還要更改hor
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.