简体   繁体   中英

No matching function call to T::T() in class template

I'm trying to write a generic template class, but I keep getting this error when I try to implement it:

no matching function for call to type_impl::type_impl()

where type_impl is the type I'm trying to use the class with.

Here's my code:

class BaseClass {
protected:
    // Some data
public:
    BaseClass(){
        // Assign to data
    }; 
};

template <class T>
class HigherClass : BaseClass {
private:
    T data;
public:
    // Compiler error is here.
    HigherClass(){};
    // Other functions interacting with data
};

class Bar {
private:
    // Some data
public:
    Bar(/* Some arguments*/) {
        // Assign some arguments to data
    };
};

// Implementation
HigherClass<Bar> foo() {
     HigherClass<Bar> newFoo;

     // Do something to newFoo

     return newFoo;
};

The problem is that, since you have provided a nondefault constructor for Bar , the compiler no longer provides a default constructor, and this is required in your code:

HigherClass(){}; // will init data using T()

So provide a default constructor for Bar . For example:

class Bar {

public:
    Bar() = default; // or code your own implementation
    Bar(/* Some arguments*/) { ... }
};

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