I'm getting this error: error C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers
I looked it up, and everywhere says the same thing, that I can't use the this pointer as a parameter for a static function, because the function doesn't belong to any particular object, so it doesn't have a this pointer. I get that, but I'm pretty sure I'm calling a member function (in main): comp_2->get_mediator().notify(this, "This is from component 2."); // line 6
I was testing to make sure my code is correct to give me the type of the object sending the notification at runtime. (see lines 1, 2 and 3)
I haven't declared anything static. I checked to make sure the method is within the brackets of the class. What am I missing?
If I run the code below without line 6, the output is:
1 2
class ComponentTwo says: This is from component 2.
class ComponentOne says: This is from component 1.
If I run line 6 (not 4 and 5), I get the error at the top of my question.
Lines 4 and 5 simply call a method that gets the mediator and calls notify, so why is there any difference? (see lines 1, 2 and 3)
Well, I don't know if this is a real fix, or a hacky hack, but consider changing line 74 to: comp_2->get_mediator().notify(comp_2, "This is from the comp_2.");
@Ganado Alright, I'll remember that. Thanks for the quick reply.
So, 'this' refers to the class surrounding the function, where it's currently begin called.
I think I assumed 'this' meant the class the function belongs to.