为什么我会收到"void value not ignored as ought to be"错误?
Why am I getting a "void value not ignored as ought to be" error?
首先,我想声明我正在处理一个家庭作业问题,因此,如果给出的任何答案不仅仅是答案,而是解释,我将不胜感激。另外,如果您担心帮助解决家庭作业问题,我只想让您知道我的老师鼓励我们使用此网站寻求帮助,因此您无需觉得自己在帮助我作弊!
无论如何,这是眼前的问题。我正在制作一个基于控制台的小型"游戏",您可以在其中尝试找到从开始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
的指针,使用您访问返回void
setNeighbor
->
,您正在取消引用它,这是非法的。我猜你的意思是做(*startLoc).setNeighbor(NORTH, passage);
这是有效的。问题源于对运营商.
和->
的误解。
当变量中有
struct
或class
对象时,可以通过.
运算符访问其成员,如下所示object.member
或object.member_func()
。但是,如果您有指向此类对象的指针,则访问它的常用方法是
object_ptr->member
或object_ptr->member_func()
。在这里->
取消引用提供对基础对象成员的访问权限的指针。当你做
*object_ptr
你有效地取回对象本身,因此你可以像使用普通(非指针)可访问(*object_ptr).member
一样访问它。
您正在做的是取消引用它两次,一次是使用->
,然后是*
.但是,编译器只接受第一个,它假设从函数返回的内容再次是一个指针,并且您正在尝试通过*
使用第二个取消引用来取消引用它。
- 为什么我会" void value not ignored as it ought to be"?
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 如何理解"Temporary objs are destroyed as the last step in evaluating the full-expression"?谁能用一些简单的例子来说明这
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误
- r-在Rcpp和C++之间转换矢量(使用Rcpp::as或Rcpp:::wrap)是否会创建一个新的矢量并复制元素
- template<class T, int N> void h(T const(&)[N]); as friend function
- 我正在"void value not ignored as it ought to be"我该怎么办?
- "expression cannot be used as a function"回报声明
- OMNeT++ cPacket as std::bitset 以应用 Reed-Solomon 编码
- 函数如何在不这样做的情况下在新线程上运行"as if"?
- C++指针算术"lvalue required as left operand of assignment"错误
- 当调用时,为什么ranges ::累积的init as std :: Move(init)
- 如何在开关语句中使用 cin "as a parameter"
- regex_match as predicate
- 尝试分配函数指针时获取"Void value not ignored as it ought to be"
- "void value not ignored as it ought to be" - 出了什么问题?
- 为什么我会收到"void value not ignored as ought to be"错误?
- "void value not ignored as it ought to be" - 夸脱/C++