【发布时间】:2021-12-31 10:27:59
【问题描述】:
由于某种原因,我不能使用附加到“代理”对象的“getNotify()”函数。我在不起作用的行中添加了注释(在“Publisher”类中)。作为一个错误,我得到“错误;不允许指向不完整类类型的指针”请帮助
“Broker”类是用 Singleton-Pattern 实现的 Broker.h 类:
#ifndef DEF_Broker
#define DEF_Broker
#include <iostream>
#include "classes.h"
using namespace std;
class Broker : Publisher
{
static Broker *instance;
Broker();
public:
static Broker* getInstance()
{
if(!instance)
{
instance = new Broker();
}
return instance;
}
void getNotify()
{
for(auto sub : SubscriberList)
{
if(t.msg == "Hello World")
{
SubCount++;
cout << SubCount << " - ";
sub->update(t.msg);
}
else if(t.msg == "Ping")
{
cout << "Ping" << endl;
sub->update("Pong");
}
}
}
};
Broker *Broker::instance = 0; // Null, because instance will be initialized on demand.
Broker::Broker(){}; // Private constructor so that no objects can be created.
#endif
classes.h:
#ifndef DEF_classes
#define DEF_classes
#include <iostream>
#include <list>
using namespace std;
class Broker;
class Subscriber
{
public:
void update(string msg)
{
cout << msg << endl;
}
};
class Topic
{
public:
string msg;
Topic(){};
Topic(string msg)
{
this->msg = msg;
}
};
class Publisher
{
protected:
list<Subscriber*> SubscriberList;
static int SubCount;
public:
Topic t;
Broker *broker;// = broker->getInstance();
Publisher(){}
Publisher(Topic t)
{
this->t = t;
};
void AddSub(Subscriber *sub)
{
SubscriberList.push_back(sub);
}
void notify(string msg)
{
broker->getNotify(); // this not working
}
};
int Publisher::SubCount = 0; // Initialize static member SubCount
#endif
【问题讨论】:
-
您似乎没有在
classes.h中包含Broker.h。如果不这样做,您将无法使用broker->getNotify()。
标签: c++ class pointers incomplete-type