简体   繁体   中英

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:

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.

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

defines a class template, a blueprint for a potential family of classes. 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 FilterNaturalString: public Filtro<std::string> 
{
...
};

defines a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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