0

I'm working on an x/y coordinate maze solving project.

I have a problem with my Maze object, whose data members are locations which also are objects of int row, int col and enum Direction {right down left up done}.

When I run my debugger, error messages come up that say "invalid use of this outside of non static member function" on the Maze object.

Why is this coming up? I don't understand how this message is showing if I'm not using any this->endLocation statement anywhere in my Maze.cpp file.

#include "maze-proj1.h"

using namespace std;

Maze::Maze() {
    validLocationCount = 0;
    validLocations = NULL;
    startLocation = endLocation;
}

Maze::~Maze() {
    if(validLocations != NULL) {
        delete [] validLocations;
    }
    validLocations = NULL;
    validLocationCount = 0;
}

Location Maze::getStartLocation() const {
    return startLocation;
}

bool Maze::isValidLocation(const Location &loc) const {
    int count = 0;
    for (int i = 0; i < validLocationCount; i++) {
        if (loc == validLocations[i]) {
            count++;
        }
    }
    if (count == validLocationCount){
        return true;
    }
    return false;
}

bool Maze::isEndLocation(const Location &loc) const {
    return loc == endLocation;
}

istream &operator>>(istream &is, Maze &m) {
    is >> m.validLocationCount;
    m.validLocations = new Location[m.validLocationCount];
    for(int i = 0; i < m.validLocationCount; i++){
        is >> m.validLocations[i];
    }
    is >> m.startLocation;
    is >> m.endLocation;
}
class Maze {
public:
    Maze(void);
    ~Maze();

    Location getStartLocation(void) const;
    bool isValidLocation(const Location &loc) const;
    bool isEndLocation(const Location &loc) const;

    friend istream &operator>>(istream &is, Maze &m);

private:
    Maze(const Maze &) { assert(false); }
    const Maze &operator=(const Maze &)
    { assert(false); return *this; }

    int validLocationCount;
    Location *validLocations;

    Location startLocation, endLocation;
};

#endif
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • "*I'm not using any this->endLocation statement anywhere in Maze.cpp file*" - yes, you are, in `isEndLocation()`. Inside a non-static class method, ALL non-static members are accessed via `this` whether you explicitly write `this->` or not. So, `return loc == endLocation;` is treated as `return loc == this->endLocation;` – Remy Lebeau Sep 18 '22 at 04:44
  • 3
    Your program has lots of other erros. Fix them and provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Jason Sep 18 '22 at 04:44
  • Will do, Thanks! I'm still new to C++ – ThomTheGreat Sep 18 '22 at 05:43
  • @JasonLiam Could you point out to one area where I could start on to fix errors? It would be awesome if you could! – ThomTheGreat Sep 18 '22 at 05:56
  • @ThomTheGreat Your overloaded `operator>>` is **missing a return statement**. You can try out your code [here online](https://onlinegdb.com/BNtgI3xcD) and you'll see that you have more errors. Just fix those errors so that we can focus on the error about which your current question is. Also, a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is recommended. These books are also available as PDFs for free. – Jason Sep 18 '22 at 06:03
  • @JasonLiam missing a return statement was a silly mistake! I must have deleted it as I was deleting every this->endLocations in my Maze.cpp file. Now it compiles on my end and is showing expected output when I put together with testMain.cpp. I tried out my code on the website you provided, and numbers of these errors are coming from missing Location.h and implementations for the Location object. To my best knowledge, I do not see lots of errors, but I still get the invalid use of this message in my debug console – ThomTheGreat Sep 18 '22 at 06:36
  • @ThomTheGreat My point is that you(as OP) has to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that reproduces the error that you're asking about. Either remove the dependency of `Location` class and then repost your code or include the `Location` class code in your question so that we don't have to worry about other errors and can first confirm that you're actually getting the error that you're asking about. – Jason Sep 18 '22 at 06:50

0 Answers0