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)
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
// error C2355: 'this': can only be referenced inside non-static member functions or
// non-static data member initializers
#include <iostream>
#include <string>
#include <typeinfo>
class BaseComponent;
class BaseMediator
{
public:
virtual void notify(BaseComponent* sent_by, std::string message) const = 0;
};
class BaseComponent
{
protected:
BaseMediator* mediator_ = nullptr;
public:
BaseComponent(BaseMediator* mediator = nullptr) : mediator_(mediator){}
void set_mediator(BaseMediator* mediator) { mediator_ = mediator; }
BaseMediator& get_mediator(){ return *mediator_; }
virtual void test_runtime_typeid() = 0; // line 1
};
class ComponentOne : public BaseComponent
{
private:
public:
void test_runtime_typeid()
{
mediator_->notify(this, "This is from component 1."); // line 2
}
};
class ComponentTwo : public BaseComponent
{
private:
public:
void test_runtime_typeid()
{
mediator_->notify(this, "This is from component 2."); // line 3
}
};
class Mediator : public BaseMediator
{
private:
ComponentOne* comp_1_ = nullptr;
ComponentTwo* comp_2_ = nullptr;
public:
void notify(BaseComponent* sent_by, std::string message) const override
{
std::cout << typeid(*sent_by).name() << " says: " << message << '\n';
}
Mediator(ComponentOne* comp_1, ComponentTwo* comp_2) : comp_1_(comp_1), comp_2_(comp_2)
{
comp_1->set_mediator(this);
comp_2->set_mediator(this);
}
};
int main()
{
ComponentOne* comp_1 = new ComponentOne();
ComponentTwo* comp_2 = new ComponentTwo();
Mediator* med = new Mediator(comp_1, comp_2);
comp_2->test_runtime_typeid(); // line 4
comp_1->test_runtime_typeid(); // line 5
//comp_2->get_mediator().notify(this, "This is from the comp_2."); // line 6 error
}
|