سؤال

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 Locations.

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.

هل كانت مفيدة؟

المحلول 2

The error seems to be here:

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

You have to keep in mind, that a->b means (*a).b. So your line of code actually means (including the operator precedence):

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

You are dereferencing Location * to get a Location, then calling a method, which returns void and then you try to dereference a void, what is forbidden.

Change these lines simply to:

startLoc->setNeighbor(NORTH, passage);

This version is also acceptable (though requires additional parenthesis):

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

نصائح أخرى

*startLoc->setNeighbor(NORTH, passage);

That line is attempting to dereference a void, which will not work. The line should read

startLoc->setNeighbor(NORTH, passage); // note the lack of *

The same goes for all of the setNeighbor calls.

*startLoc->setNeighbor(NORTH, passage);

startLoc is a pointer to Location, using -> you access setNeighbor which returns void, you're dereferencing it, which is illegal. I guess you mean to do (*startLoc).setNeighbor(NORTH, passage); which is valid. The issue stems from misunderstanding the operators . and ->.

  • When you've a struct or class object in a variable, its members can be accessed via the . operator like this object.member or object.member_func().

  • However, if you've a pointer to such an object, then the usual way of accessing it is object_ptr->member or object_ptr->member_func(). Here -> dereferences the pointer giving access to the underlying object's members.

  • When you do *object_ptr you effectively get back the object itself, and thus you can access it like you do with an ordinary (non-pointer) vairable (*object_ptr).member.

What you're doing is dereferencing it twice, once with -> and then with *. However, the compiler takes only the first and it assumes what is returned from the function is again a pointer and that you're trying to dereference it using the second dereferencing via * .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top