为什么我会收到"void value not ignored as ought to be"错误?

Why am I getting a "void value not ignored as ought to be" error?

本文关键字:ought as to be 错误 ignored value 为什么 void not      更新时间:2023-10-16

首先,我想声明我正在处理一个家庭作业问题,因此,如果给出的任何答案不仅仅是答案,而是解释,我将不胜感激。另外,如果您担心帮助解决家庭作业问题,我只想让您知道我的老师鼓励我们使用此网站寻求帮助,因此您无需觉得自己在帮助我作弊!

无论如何,这是眼前的问题。我正在制作一个基于控制台的小型"游戏",您可以在其中尝试找到从开始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 值未按应忽略"。我已经研究了这个错误,发现最常见的原因是当程序尝试使用函数时,就好像它返回一个值一样,但我看不出我可以在哪里这样做。这是我的函数的示例:

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:UsersZacharyDocumentsSchoolCS162Locationmain.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调用也是如此。

错误似乎在这里:

*startLoc->setNeighbor(NORTH, passage);
...

你必须记住,a->b意味着(*a).b.所以你的代码行实际上意味着(包括运算符优先级):

*((*startLoc).setNeighbor(NORTH, passage));

您正在取消引用Location *以获取Location,然后调用一个返回void的方法,然后您尝试取消引用void,这是禁止的。

将这些行简单地更改为:

startLoc->setNeighbor(NORTH, passage);

此版本也是可以接受的(尽管需要额外的括号):

(*startloc).setNeighbor(NORTH, passage);
*startLoc->setNeighbor(NORTH, passage);

startLoc是一个指向Location的指针,使用您访问返回voidsetNeighbor->,您正在取消引用它,这是非法的。我猜你的意思是做(*startLoc).setNeighbor(NORTH, passage);这是有效的。问题源于对运营商.->的误解。

  • 当变量中有structclass对象时,可以通过.运算符访问其成员,如下所示object.memberobject.member_func()

  • 但是,如果您有指向此类对象的指针,则访问它的常用方法是object_ptr->memberobject_ptr->member_func()。在这里->取消引用提供对基础对象成员的访问权限的指针。

  • 当你做*object_ptr你有效地取回对象本身,因此你可以像使用普通(非指针)可访问(*object_ptr).member一样访问它。

您正在做的是取消引用它两次,一次是使用->,然后是*.但是,编译器只接受第一个,它假设从函数返回的内容再次是一个指针,并且您正在尝试通过*使用第二个取消引用来取消引用它。

相关文章: