简体   繁体   English

C++ 继承模板成员的使用方法

[英]C++ using method of inherited template member

I tried to have a class with a template member.我试图用模板成员创建一个 class。 The template member must be an inherited class. I have error when I call the class methods.模板成员必须是继承的class。调用class方法时出错。 My code is:我的代码是:

#include <iostream>
#include <string>
#include <typeinfo>

class Base
{
public:
    Base (std::string param) {text=param;}
    virtual ~Base() {};
    virtual void print(){std::cout<<text<<std::endl;};

    std::string text;
    };
class Child1 : public Base
{
public:
    Child1(const std::string& param):Base(param){ }
    ~Child1() {}

};
class Child2 : public Base
{
public:
    Child2(const std::string& param):Base(param){ }
    ~Child2() {}

};

template <typename T>
class Adapter
{
public:
    Adapter(){};
    const T* get() const {return value;}
    void setValue(T* rhs) {value=rhs;}    
    void print(){value->print();}
    void test(){std::cout<<"Adapter method"<<std::endl;}
private:
    T* value;

    };

int main() {



  Child1 ex1("name1");
  Child2 ex2("name2");

  Adapter<Base *> adapt;
  Adapter<Child1 *> adapt1;
  Adapter<Child2 *> adapt2;
  
  std::cout << typeid(adapt2).name() << std::endl;
  adapt2.test();
  adapt1.setValue(&ex1);
  adapt1.print();
}

I have these error:我有这些错误:

 In function 'int main()':
67:23: error: no matching function for call to 'Adapter<Child1*>::setValue(Child1*)'
67:23: note: candidate is:

47:11: note: void Adapter<T>::setValue(T*) [with T = Child1*]
47:11: note:   no known conversion for argument 1 from 'Child1*' to 'Child1**'
 In instantiation of 'void Adapter<T>::print() [with T = Child1*]':
68:16:   required from here

48:18: error: request for member 'print' in '*((Adapter<Child1*>*)this)->Adapter<Child1*>::value', which is of pointer type 'Child1*' (maybe you meant to use '->' ?)

Could you help me to find my errors?你能帮我找出我的错误吗?

Could you help me to find my errors?你能帮我找出我的错误吗?

The first error :一个错误

error: cannot convert ‘Child1*’ to ‘Child1**

is because &ex1 is of type Child1* but adapt1 is of type Adapter<Child1*> meaning that the corresponding member function setValue takes a parameter of type T* = Child** .是因为&ex1是 Child1 Child1*类型,而adapt1Adapter<Child1*>类型,这意味着相应的成员 function setValue接受类型T* = Child**的参数。 Essentially, you're passing a Child* to setValue while it expects a Child** .本质上,您将Child*传递给setValue而它需要Child** That is, the type of the actual argument you are passing does not matches with the parameter type of the member function.即你传递的实参类型与成员function的参数类型不匹配。


The second error :第二个错误

error: request for member ‘print’ in ‘*((Adapter*)this)->Adapter::value’, which is of pointer type ‘Child1*’

is because the data member value is of type T* = Child** and thus when you wrote:是因为数据成员value的类型为T* = Child** ,因此当您编写时:

value->print(); //this is equivalen to (*value).print();

you're trying to call a member function named print on *value which is of type Child1* .您正在尝试在类型为Child1* * 的*value上调用名为print的成员 function。 But Child1* is a pointer type and has no member functions.但是Child1*是指针类型,没有成员函数。 Thus you get the second mentioned error.因此你得到了第二个提到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM