Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;示例观察者模板类错误 我在Windows上用C++创建了观察模板样例。_C++_Templates_Observer Pattern - Fatal编程技术网

C++;示例观察者模板类错误 我在Windows上用C++创建了观察模板样例。

C++;示例观察者模板类错误 我在Windows上用C++创建了观察模板样例。,c++,templates,observer-pattern,C++,Templates,Observer Pattern,这里有一个代理人,他有一份客户名单。每当代理的实体(变量x)发生更改时,它都会通知其客户,并将x的值传递给客户。然后,客户将该值存储在各自的变量中 在下面的代码中,代理作为主体,客户作为观察者。 代理是从其代理模板类创建的,客户是从其客户模板类创建的 template <typename T> class customer // acts as base observer class { char name[50]; public:

这里有一个代理人,他有一份客户名单。每当代理的实体(变量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);
}
模板
类customer//充当基本观察者类
{
字符名[50];
公众:
客户()
{

cout通过这样创建代理类,addCust的有效签名变为
void addCust(customer*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{};
结构customerC:customer{};
结构customerD:customer{};
结构代理
{
void addCust(客户*customer){…}
std::向量列表;
};
int main()
{
代理人a;
顾客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);
}
struct-customer{};
结构customerC:customer{};
结构customerD:customer{};
模板
结构代理
{
void addCust(CustomerT*customer){…}
std::向量列表;
};
int main()
{
代理人a;
顾客c;
a、 addCust&c;
}
错误的直接原因是
addCust
需要一个指针,但
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);
}