简体   繁体   中英

Scope problems with inherited non-template class (template base, non-template derived)

I am trying to create a templated base class with non-templated derived classes. I have been following umsl.edu/~subramaniana/templates8.html and http://www.cplusplus.com/doc/tutorial/templates/ to do so.

template <class Type>
class Base {
protected:
    std::string line;
public:
    Base();
};

class DerivedA : public Base<T> {
    //error: 'T' was not declared in this scope
    //error: template argument 1 is invalid
public:
    DerivedA();
protected:
    std::list<std::string> A;
};

I think I am missing something fundamental about how this all works, but I can't seem to grasp it.

This is the full header and implementation:

http://ideone.com/H9NXdw

You missed template<typename T> in class DerivedA declaration. Base is a template, you need to provide template parameter to it.

template<typename T> 
class DerivedA : public Base<T> 

Or you could let DerivedA derive from a certain type of Base, for example:

 class DerivedA : public Base<int>

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