Clarification request on use of 'this->'

Greetings,
I'm working on a accessor method for my class MobileRobot.
The method location() should return a r_val pointer to a Coordinate.
location_ is a protected member of MobileRobot class. With that said I am unsure why I'm receiving the following error.

ERROR: invalid use of 'this' in non-member function //see line 3

Upon removing 'this->' from line 3 I am met with a new error.

ERROR: 'location_' was not declared in this scope

If anyone can help me understand how I have misused 'this->' or my scoping error, it would be greatly appreciated.



1
2
3
4
5
6
7
8
9
10
11
12
13
1   //MobileRobot.cc:
2   const Coordinate* location(){
3     Coordinate* r_val = this->location_;
4      return r_val = location_;
5   } 
6 
7   //MobileRobot.h
8   class MobileRobot{
9     public:
10      const Coordinate* location();
11    protected:
12      Coordinate *location_;
13  }
Inside a class function, "this" is a pointer that points to the particualr instance of the class. You don't need to use it.

The method location() should return a r_val pointer to a Coordinate.

That seems unusual. Is that the specific instruction in your assignment?


When writing a member function, you have to speficy what class it's a member of:

//MobileRobot.cc:
1
2
3
const Coordinate* MobileRobot::location()
{
  ...
Last edited on
I question the use of the pointers, why not just use a "normal" non-pointer instance of Coordinate?

And how and where are you allocating memory for location_?





@Repeater

Thank you for your assistance! Scoping has been giving me quite a bit of trouble.
Your solution solved my issue here, and you where correct about r_val being part of the assignment.
Topic archived. No new replies allowed.