First, I would like to state that I am working on a homework problem, and as a result of this I would really appreciate it if any answers given were not simply answers, but explanations. Also, if you're worried about helping with homework problems, I just want you to know that my instructor has encouraged us to use this site for help, so you don't need to feel like you're helping me cheat!
Anyway, here is the problem at hand. I am making a small console-based "game" where you try to find your way from a start Location
to an end Location
. The Location
header file looks like this:
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();
};
The basic idea of the program is that each Location
has four other possible Location
's it can be linked to. The goal of the "game" is to have the player start at one location, in this case it is "a deep, dark cave" and end up on the surface by traversing through these Location
s.
To set this up, I have a function void buildMap(Location* startLoc, Location* endLoc)
that generates the entire scenario. The problem I am facing comes in during this function, and I don't understand what the problem is. I am getting the error "void value not ignored as ought to" for every line with the setNeighbor() function. I have looked into this error and found that the most common cause of this is when a program tries to use a function as if it were returning a value, but I don't see where I could be doing that. Here is an example of my function:
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);
}
If it helps, here is the setNeighbor
function as well:
void Location::setNeighbor(Direction dir, Location* neighborLoc){
neighbors[dir] = neighborLoc;
}
And here is an example of the error that I am recieving. It happens on each similar line with the same error.
C:\Users\Zachary\Documents\School\CS162\Location\main.cpp:30: error: void value not ignored as it ought to be
*startLoc->setNeighbor(NORTH, passage);
Any help that can be given to solve this error is much appreciated, and if any more information is needed please leave a comment so I can help you to help me.