【发布时间】:2014-11-15 18:58:12
【问题描述】:
我有这个 Pirate.cpp 子类,我希望它的 mov 函数从 main 访问一个数组。
这个数组基本上就是地图(检查它是否已经有船,停靠并访问数组以将对象(海盗船)的位置从map[1][1]更改为map[1][2]。
这张地图是一个二维的海对象数组,每个人都可以用指针持有一艘船。
Ship 是 pirate 的父类(在将 Pirate 插入 Ship 指针之前,我还有其他子类我会做 dynamic_cast)。
我有一个错误代码:'Map' , 'Place' was not declared in this scope,我理解。
那么基本上我该如何处理这个范围问题?有其他方法吗?
海盗.cpp
#include "pirate.h"
#include "Sea.h"
void Pirate::mov()
{
MAP[1][2]->(*place)=MAP[1][1]->(*place)
MAP[1][2]->(*place)=NULL
}
海.h
#ifndef SEA.H
#define SEA.H
#include "ship.h"
class Sea
{
private:
bool hasShip;
bool isDock;
protected:
Ship *place = NULL;
public:
bool gethasShip() const {return hasShip;}
bool getisDock() const {return isDock;}
void sethasShip(bool i) {hasShip = i;}
void setisDock(bool i) {isDock = i;}
};
#endif
主要
#include <iostream>
#include <vector>
#include <string>
#include "Ship.h"
#include "Sea.h"
#define SIZE 100
using std::cout;
using std::vector;
extern Sea Map[SIZE][SIZE];
int main()
{
Sea Map[SIZE][SIZE];
}
【问题讨论】:
-
我建议你获取变量
bool Sea::hasShip和void Sea::sethasShip(bool)的 rif - 当place是NULL时,你没有Ship,不是吗?
标签: c++ arrays function pointers compiler-errors