gpt4 book ai didi

C++ 示例观察者模板类错误

转载 作者:行者123 更新时间:2023-11-28 04:51:55 25 4
gpt4 key购买 nike

我正在 Windows 上用 C++ 创建观察者模板示例。

这里有一个具有客户列表的代理。每当代理的实体(变量 x)发生更改时,它都会通知其客户,并将 x 的值传递给客户。然后,客户将此值存储在他们各自的变量中。

在下面的代码中,代理作为主体,客户作为观察者。代理是从他们的代理模板类创建的,客户是从他们的客户模板类创建的。

template <typename T>
class customer // acts as base observer class
{
char name[50];

public:
customer()
{
cout << __FUNCTION__ "(): " << "DEFAULT CONS\n";
}

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 cons\n";
}

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 cons\n";
}

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 cons\n";
}

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 << "\n\nYou 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 << "\n\nYou 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 cons\n";
}

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() 函数时,我传递了观察者对象。

最佳答案

通过以这种方式创建您的 agentS 类,addCust 的有效签名变为 void addCust(customer<agentS>* cobj); .但是,您的客户类并未根据代理类型进行模板化(实际上似乎没有理由对其进行模板化)。

您似乎混合了动态多态性(继承和虚拟函数与客户)和静态多态性(用于创建一种客户类型的 vector 的模板)。这些选项中的任何一个都更有意义:

动态多态性(继承)。您可以将不同类型的客户存储在同一个容器中,通过存储基类指针,并使用客户基类和虚函数以相同的方式处理它们:

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);
}

静态多态性(模板)。代理类以客户类型为模板,因此 vector 只能包含一种类型的客户,但很容易为任何给定的客户类型创建特定的代理:

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);
}

关于C++ 示例观察者模板类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com