多维数组问题(边界)
Posted
技术标签:
【中文标题】多维数组问题(边界)【英文标题】:Multidimensional Array trouble (bounds) 【发布时间】:2011-06-07 17:29:03 【问题描述】:这是有问题的代码:
#ifndef WEAPONS_H_INCLUDED
#define WEAPONS_H_INCLUDED
#include "Battleship header.h"
void Player::torpedo(string enemyEvtTracker[][10], string myhitboard[][10])
string bcn; //board column number
cout << "Enter board column number (1-9): "; cin >> bcn; flush();
if(bcn!="1"&&bcn!="2"&&bcn!="3"&&bcn!="4"&&bcn!="5"&&bcn!="6"&&bcn!="7"&&bcn!="8"&&bcn!="9")
cout <<endl<< "That is not a valid number.";
return;
#endif // WEAPONS_H_INCLUDED
这里是 Player 类:
#ifndef BATTLESHIPPLAYERCLASS_H_INCLUDED
#define BATTLESHIPPLAYERCLASS_H_INCLUDED
using namespace std;
class Player // define a class for a player
void torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10]);
void cannon();
void scatter();
void menu();
friend int main(int, char*[]); //Let main access the protected members
friend int routine_END(void);
public:
void displaydata()
cout << money << endl << gunpowder << endl << ammo_cannon << endl << ammo_4hit << endl;
string savename;
int gunpowder;
int ammo_cannon;
int ammo_4hit; string gun_4;
int ammo_scatter; string gun_s;
int money;
Player(string name)money=18000; gunpowder=100;ammo_cannon=20; ammo_4hit=0; ammo_scatter=0; gun_4="OFF"; gun_s="OFF";playername=name; //Define the constructor
void simplegame(void) gunpowder=99999999; ammo_cannon=999999999; ammo_scatter=4; gun_s="ON";
void getname(string *playername, int option)
if (option==1)
cout << "Enter your name here player 1:"; cin >> *playername;
else cout << "Enter your name here player 2:"; cin >> *playername;
string playername;
char mainRowGuess;
int check_transaction (int mymoney, int moneyowed)
if (mymoney-moneyowed<<0) return 0;
else return 1;
void change_Answer_to_number(char row,int* outputRow)
if (row=='A'||row=='a')*outputRow =1;
else if (row=='B'||row=='b')*outputRow =2;
else if (row=='C'||row=='c')*outputRow =3;
else if (row=='D'||row=='d')*outputRow =4;
else if (row=='E'||row=='e')*outputRow =5;
else if (row=='F'||row=='f')*outputRow =6;
else if (row=='G'||row=='g')*outputRow =7;
else if (row=='H'||row=='h')*outputRow =8;
else if (row=='I'||row=='i')*outputRow =9;
else *outputRow = 0;
void changeCharToNumber(char row, int* outputRow)
if (row=='1')*outputRow=1;
else if (row=='2')*outputRow=2;
else if (row=='3')*outputRow=3;
else if (row=='4')*outputRow=4;
else if (row=='5')*outputRow=5;
else if (row=='6')*outputRow=6;
else if (row=='7')*outputRow=7;
else if (row=='8')*outputRow=8;
else if (row=='9')*outputRow=9;
else cout << "Unexpected Error." << endl; *outputRow=0;
char airRowStart; char airColStart; char aircraftDirec;
char destRowStart; char destColStart; char destroyerDirec;
char subRowStart; char subColStart; char subDirec;
char patrolStart; char patrolDirec;
/// START MENU FUNCTION
void error_money(void) cout << "Not enough money!";
char startRowAircraftCarrier;
char startRowDestroyer;
char startRowSub;
char startRowPatrolBoat;
friend int routine_END (void);
friend void menu (int* gunpowder, int* ammo_cannon, int* ammo_4hit, int* ammo_scatter, int* money, string* gun_4, string* gun_s);
;
#endif // BATTLESHIPPLAYERCLASS_H_INCLUDED
这会生成以下构建日志...
------------- 构建:高级战舰对象中的调试
[ 50.0%] 编译:main.cpp C:\Advanced_Battleship_Revised_5111\main.cpp:32: 警告:忽略 #pragma 注释 包含的文件 C:\Advanced_Battleship_Revised_5111\/impputoutput.h:9, 来自 C:\Advanced_Battleship_Revised_5111\/Battleship header.h:3, 来自 C:\Advanced_Battleship_Revised_5111\main.cpp:25: C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:74: 警告:“introutine_END()”是 已经是“玩家”班的朋友 C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h: 在成员函数'int 播放器::check_transaction(int, int)': C:\Advanced_Battleship_Revised_5111\/BattleshipPlayerClass.h:33: 警告:建议使用括号 '-' inside '> C:\Advanced_Battleship_Revised_5111\/weapons.h:5:错误:声明 'enemyEvtTracker' 作为多维 数组必须对所有人都有边界 除了第一个尺寸 C:\Advanced_Battleship_Revised_5111\/weapons.h:5: 错误:在 ',' 标记之前预期 ')' C:\Advanced_Battleship_Revised_5111\/weapons.h:5:错误:预期的构造函数, 析构函数,或之前的类型转换 ',' 记号 C:\Advanced_Battleship_Revised_5111\/weapons.h:5:错误:预期的构造函数, 析构函数,或之前的类型转换 'myhitboard' 进程终止于 状态 1(0 分 1 秒)4 错误,5 个警告
【问题讨论】:
与您的问题无关,但您确实应该将 bcn 转换为整数,然后测试 if (bcn = 9) 以输出错误消息。 贴出Player
类的定义。
为什么Player::torpedo
缺少返回类型?
不,还是不行。这很奇怪,因为它确实有界限
@someguy 发布确切的错误,行号。和所有。并确保.cpp 文件中torpedo
的返回类型也是void
。
【参考方案1】:
Player::torpedo(string enemyEvtTracker[10][10], string myhitboard[10][10])
//..
这是成员函数的定义。 声明在哪里?
我的猜测是,在声明中你没有提到数组的大小,就像你在定义中所做的那样。看来你写的只是enemyEvtTracker [][]
?查看Player
类的定义,并验证您是如何在其中声明torpedo
成员函数的。
【讨论】:
抱歉播放器类定义搞砸了。它不会正确格式化 哦,我忘了在原型中包含参数(数组,数组)。以上是关于多维数组问题(边界)的主要内容,如果未能解决你的问题,请参考以下文章