Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类模板中没有对T::T()的匹配函数调用_C++_Templates - Fatal编程技术网

C++ 类模板中没有对T::T()的匹配函数调用

C++ 类模板中没有对T::T()的匹配函数调用,c++,templates,C++,Templates,我正在尝试编写一个通用模板类,但在尝试实现它时,我不断遇到以下错误: no matching function for call to type_impl::type_impl() 其中type_impl是我试图使用该类的类型 这是我的密码: class BaseClass { protected: // Some data public: BaseClass(){ // Assign to data }; }; template <class

我正在尝试编写一个通用模板类,但在尝试实现它时,我不断遇到以下错误:

no matching function for call to type_impl::type_impl()
其中type_impl是我试图使用该类的类型

这是我的密码:

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;
};
类基类{
受保护的:
//一些数据
公众:
基类(){
//分配给数据
}; 
};
模板
类高级类:基类{
私人:
T数据;
公众:
//编译器错误在这里。
高级类(){};
//与数据交互的其他函数
};
分类栏{
私人:
//一些数据
公众:
条(/*某些参数*/){
//为数据分配一些参数
};
};
//实施
高级类foo(){
高级纽福;
//对纽福做点什么
返回newFoo;
};

问题在于,由于您为
Bar
提供了非默认构造函数,编译器不再提供默认构造函数,这是您的代码所必需的:

HigherClass(){}; // will init data using T()
因此,为
Bar
提供一个默认构造函数。例如:

class Bar {

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

问题是,由于您已为
Bar
提供了非默认构造函数,编译器不再提供默认构造函数,这是您的代码所必需的:

HigherClass(){}; // will init data using T()
因此,为
Bar
提供一个默认构造函数。例如:

class Bar {

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

您可能想补充一下为什么会出现这种情况。例如,“当您尝试创建
高级类
时,它有一个成员
条形数据
,需要创建一个默认构造函数。”@clcto谢谢,我正在这样做。我希望现在情况更清楚了。你可能想补充一下为什么会这样。例如,“当您尝试创建
高级类
时,它有一个成员
条形数据
,需要创建一个默认构造函数。”@clcto谢谢,我正在这样做。我希望现在更清楚了。