首先,我想声明我正在处理一个家庭作业问题,因此,如果给出的任何答案不仅仅是答案,而是,我将不胜感激解释。另外,如果你担心帮助解决家庭作业问题,我只是想让你知道,我的老师鼓励我们使用这个网站寻求帮助,所以你不必觉得你在帮我作弊!
无论如何,这是手头的问题。我正在制作一个基于控制台的小型“游戏”,您可以在其中尝试找到从起点 Location
到终点 Location
的方式。 Location
头文件如下所示:
enum Direction {NORTH, SOUTH, EAST, WEST};
class Location{
string name;
bool visited;
Location* neighbors[4];
public:
Location();
Location(string locName);
string getDescription();
bool hasNeighbor(Direction dir);
Location* getNeighbor(Direction dir);
void setNeighbor(Direction dir, Location* neighborLoc);
string getName();
void setName(string newName);
bool visit();
bool getVisited();
};
该程序的基本思想是每个 Location
都有四个其他可能的 Location
可以链接到。 “游戏”的目标是让玩家从一个位置开始,在本例中是“一个又深又黑的洞穴”,并通过遍历这些 Location
最终到达地表。
为了设置它,我有一个生成整个场景的函数 void buildMap(Location* startLoc, Location* endLoc)
。我面临的问题是在这个函数中出现的,我不明白问题是什么。对于使用 setNeighbor() 函数的每一行,我都收到错误“void value not ignored as ought to”。我调查了这个错误,发现最常见的原因是当一个程序试图使用一个函数时,就好像它正在返回一个值,但我不知道我可以在哪里做。这是我的函数的示例:
void buildMap(Location* startLoc, Location* endLoc){
//Initialize all of the locations on the heap with temporary pointers
startLoc = new Location("a deep, dark cave");
endLoc = new Location("the surface");
Location* passage = new Location("a musty passage");
Location* shaft = new Location("a twisting shaft");
Location* alcove = new Location("a dusty alcove");
Location* toSurface = new Location("a passage to the surface");
Location* cavern = new Location("a collapsed cavern");
Location* shore = new Location("the shores of an underground lake");
//Set up the deep, dark, cave's neighbors
*startLoc->setNeighbor(NORTH, passage); //IDE changed my "." to a "->"?
*startLoc->setNeighbor(EAST, shore);
*startLoc->setNeighbor(SOUTH, cavern);
//Set up the musty passage's neighbors
*passage->setNeighbor(EAST, shaft);
*passage->setNeighbor(SOUTH, startLoc);
//Set up the twisting shaft's neighbors
*shaft->setNeighbor(EAST, alcove);
*shaft->setNeighbor(SOUTH, shore);
//Set up the dusty alcove's neighbors
*alcove->setNeighbor(SOUTH, toSurface);
//Set up the passage to the surface's neighbors
*toSurface->setNeighbor(NORTH, alcove);
*toSurface->setNeighbor(WEST, endLoc);
//Set up the collapsed cavern's neighbors
*toSurface->setNeighbor(NORTH, startLoc);
//Set up the shore's neighbors
*toSurface->setNeighbor(NORTH, shaft);
*toSurface->setNeighbor(WEST, startLoc);
}
如果有帮助,这里还有 setNeighbor
函数:
void Location::setNeighbor(Direction dir, Location* neighborLoc){
neighbors[dir] = neighborLoc;
}
这是我收到的错误示例。它发生在具有相同错误的每个相似行上。
C:\Users\Zachary\Documents\School\CS162\Location\main.cpp:30: error: void value not ignored as it ought to be
*startLoc->setNeighbor(NORTH, passage);
非常感谢可以提供解决此错误的任何帮助,如果需要更多信息,请发表评论,以便我可以帮助您。
最佳答案
*startLoc->setNeighbor(NORTH, passage);
该行试图取消对 void
的引用,这是行不通的。该行应显示为
startLoc->setNeighbor(NORTH, passage); // note the lack of *
所有 setNeighbor
调用也是如此。
关于c++ - 为什么我会收到 "void value not ignored as ought to be"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21276166/