C 样本观察者模板类误差
C++ sample Observer Template Class error
我正在Windows上的C 创建观察者模板样本。
这里有一个具有客户列表的代理。每当代理的实体(变量X)更改时,它就会通知其客户,并将X的值传递给客户。然后,客户将此值存储在各自的变量中。
在以下代码中,代理充当主题,客户充当观察者。代理是从其代理模板类创建的,客户是从其客户模板类创建的。
template <typename T>
class customer // acts as base observer class
{
char name[50];
public:
customer()
{
cout << __FUNCTION__ "(): " << "DEFAULT CONSn";
}
customer(char* nm)
{
strcpy_s(name, nm);
cout << __FUNCTION__ "(): " << "name set to " << name << "n";
}
char * getName()
{
return(name);
}
virtual void update(int c)
{
}
};
class customerC: public customer<customerC>
{
int c;
public:
customerC()
{
cout << __FUNCTION__ "(): " << "DEFAULT customerc consn";
}
customerC(char* nm):customer<customerC>(nm)
{
cout << __FUNCTION__ "(): " << "customer is " << getName() << "n";
}
void update(int val)
{
cout << __FUNCTION__ "(): c to " << c << "n";
c = val;
}
};
class customerD: public customer<customerD>
{
int d;
public:
customerD()
{
cout << __FUNCTION__ "(): " << "DEFAULT customerd consn";
}
customerD(char* nm):customer<customerD>(nm)
{
cout << __FUNCTION__ "(): " << "customer is " << getName() << "n";
}
void update(int val)
{
cout << __FUNCTION__ "(): c to " << d << "n";
d = val;
}
};
template<typename T>
class agent
{
char name[50];
int x;
protected:
vector<customer<T>*> custList;
public:
agent()
{
cout << __FUNCTION__ "(): " << "DEFAULT agent consn";
}
virtual void setx(int c)
{
cout << __FUNCTION__ "(): " << "Setting x to " << c << "n";
//// x = c;
//// notifyObs();
}
virtual void getx()
{
cout << __FUNCTION__ "(): " << "x = " << x << "n";
}
void addCust(customer<T>* cobj)
{
cout << __FUNCTION__ "(): " << "Adding customer " << cobj->getName() << " to list.n";
custList.push_back(cobj);
}
void showCust()
{
cout << __FUNCTION__ "(): " << "Customers are:n";
if(custList.empty())
cout << "nnYou have no items.";
else
{
vector<customer<T>*>::iterator cs;
for(cs = custList.begin(); cs != custList.end(); ++cs)
{
cout << (*cs)->getName() << "n";
}
}
}
int notifyObs()
{
cout << __FUNCTION__ "(): " << "Customers notified are:n";
if(custList.empty())
cout << "nnYou have no items.";
else
{
vector<customer<T>*>::iterator cs;
for(cs = custList.begin(); cs != custList.end(); ++cs)
{
cout << (*cs)->getName() << "n";
(*cs)->update(x);
}
}
return 0;
}
};
class agentS: public agent<agentS>
{
int x;
public:
agentS()
{
cout << __FUNCTION__ "(): " << "DEFAULT agentS consn";
}
void setx(int c)
{
cout << __FUNCTION__ "(): " << "Setting x to " << c << "n";
x = c;
notifyObs();
}
void getx()
{
cout << __FUNCTION__ "(): " << "x = " << x << "n";
}
};
int _tmain(int argc, _TCHAR* argv[])
{
customerC cobj("c1");
customerD dobj("c2");
agentS agS;
agS.addCust(cobj);
//// agS.addCust<customer<customerC>>(cobj);
//// agS.addCust(dobj);
agS.showCust();
agS.setx(4);
return(0);
}
我得到编译错误
error C2664: 'agent<T>::addCust' : cannot convert parameter 1 from 'customerC' to 'customer<T> *'
我知道我称为AddCust的方式是错误的,但仍然没有任何想法来称呼它。有任何提示解决此问题吗?
我创建代理类的方式也正确吗?
class agentS: public agent<agentS>
当我调用addcust()函数时,我通过观察者对象。
通过以这种方式创建代理类,AddCust的有效签名成为void addCust(customer<agentS>* cobj);
。但是,您的客户类并未在代理类型上进行模板(实际上似乎并没有模板的原因)。
您似乎正在将动态多态性(继承和虚拟功能与客户与静态多态性(创建一种客户的向量)相结合。这些选项中的任何一个都会更有意义:
动态多态性(继承)。您可以通过存储基类指针,并使用客户群和虚拟功能以相同的方式将其存储在同一容器中:
struct customer {};
struct customerC : customer {};
struct customerD : customer {};
struct agent
{
void addCust(customer* customer) { ... }
std::vector<customer*> custList;
};
int main()
{
agent a;
customerC c;
a.addCust(&c);
}
静态多态性(模板)。代理类是在客户类型上模板的,因此向量只能包含一种类型的客户,但是很容易为任何给定的客户类型创建特定代理:
struct customer {};
struct customerC : customer {};
struct customerD : customer {};
template<CustomerT>
struct agent
{
void addCust(CustomerT* customer) { ... }
std::vector<CustomerT*> custList;
};
int main()
{
agent<customerC> a;
customerC c;
a.addCust(&c);
}
相关文章:
- 为什么在浮点中从大到小会引入更多的误差
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 使用源读取器从网络摄像头源获取 H264 样本
- 基于相邻元素 c++ 的分段误差范围的循环
- 当相机接近模型时,SSAO样本内核会导致性能下降?
- QImage::p ixel 和 QImage::setPixel 坐标超出范围误差
- 如何从 std::set 绘制 n 个元素的样本
- 将误差线添加到 VTK 二维散点图
- 动态矩阵特征分解过程中的误差
- 如何通过一次运行将多个输入样本馈送到C++张量流模型
- 如何测量一个音频样本中的低音量
- 转换为非标量误差是什么意思?我该如何解决?
- 特征误差:INVALID_MATRIX_PRODUCT乘向量和矩阵
- 使用不同参数匹配器的同一方法ON_CALL AND EXPECT_CALL会产生饱和和活动误差
- boost odeint 中的受控误差步进器是否支持复杂的数据类型?
- 如何从 avi 文件中抓取所有帧 - 如何修改 MS 样本采集卡样本
- 此外,在计算大斐波那契数时存在精度误差
- 为什么当只有样本计数不同时avcodec_fill_audio_frame返回 -22?
- C++17中不定参数函数的误差
- C 样本观察者模板类误差