简体   繁体   English

如何用链 inheritance c++ 解决这个问题?

[英]How can I solve this problem with chain inheritance c++?

Basically I'm studying about Chain Inheritance in my college and I'm expected to build a program with this, the problem I have is in this part:基本上我正在我的大学里研究Chain Inheritance,我希望用这个来构建一个程序,我遇到的问题是这部分:

template <class NUM_TYPE>
class FilterPositiveNumber: public Filtro<NUM_TYPE> {
  bool dadoValido(NUM_TYPE& d) const override {
    // TODO: Implemente este metodo.
    if (d > 0){
        return true;
    }else{
        return false;   
    }
  }
};
class FilterNaturalString: public Filtro<std::string> {
  bool dadoValido(std::string& d) const override {
    std::string::const_iterator it = d.begin();
    while (it != d.end() && std::isdigit(*it)) ++it;
    return !d.empty() && it == d.end();
  }
};

void convert2int(std::vector<std::string>& in, std::vector<int>& out) {
    Filtro<std::string>* new_check;
    new_check = new FilterNaturalString;
    for(std::string i : in){
        if(new_check->dadoValido(i)){
            out.push_back(stoi(i));
        }
      
    }
}
template <class NUM_TYPE> void test_filter_square_roots() {
    std::vector<unsigned> entrada;
    std::vector<unsigned> saida;
    read_input(entrada);
    Filtro<int>* new_check;
    new_check = new FilterPositiveNumber;
}

I get this error我收到这个错误

error: invalid use of template-name ‘FilterPositiveNumber’ without an argument list
     new_check = new FilterPositiveNumber;

I don't understant why I get a error in the second time I try to instantiate a "Filtro" if I called it the exact same way when doing it to FilterNaturalString.我不明白为什么我在第二次尝试实例化“Filtro”时遇到错误,如果我在对 FilterNaturalString 执行它时以完全相同的方式调用它。

template <class NUM_TYPE>
class FilterPositiveNumber: public Filtro<NUM_TYPE>
{
...
};

defines a class template, a blueprint for a potential family of classes.定义了一个 class 模板,一个潜在的类族的蓝图。 To use it you must tell the compiler, or the compiler must be able to infer, what type the template is to be specialized on in order to become a class.要使用它,您必须告诉编译器,或者编译器必须能够推断,模板要专门用于什么类型才能成为 class。

class FilterNaturalString: public Filtro<std::string> 
{
...
};

defines a class.定义了一个 class。

So所以

new_check = new FilterNaturalString;

is valid but有效但

new_check = new FilterPositiveNumber<int>;

is required to allow the compiler to fill in the blanks and create a class that can be instantiated from the template.需要允许编译器填充空白并创建可以从模板实例化的 class。

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

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