【发布时间】:2014-01-13 06:27:04
【问题描述】:
我对 C++ 还很陌生,并且无法将指针从一个类指向另一个类。这就是我所拥有的,它编译没有错误,但没有按我想要的方式工作。
JungleMap *Map;
class JungleMap
{
public:
void goNorth()
{
cout << "You are going north towards the river.\n";
delete[] Map;
RiverMap *Map;
}
}
class RiverMap
{
public:
void goNorth()
{
cout << "You are going north away from the river.\n";
delete[] Map;
JungleMap *Map;
}
}
int main()
{
Map->goNorth();
Map->goNorth();
}
输出如下:
You are going north towards the river.
You are going north towards the river.
这就是我想要的输出:
You are going north towards the river.
You are going north away from the river.
我如何实现这一目标?这真的让我很烦,特别是因为它编译没有问题。
【问题讨论】:
-
您的代码无效,不应编译。
-
您可能希望在定义
JungleMap之前将RiverMap声明为一个类。声明为class RiverMap;。 -
如何编译没有问题,这段代码是自包含的吗?
-
仅供参考:ideone.com/GnNFnk
-
抛开编译错误,您缺少的是地图的基类。阅读更多关于 C++ 继承的内容。
标签: c++ class pointers delete-operator