gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:55:10 24 4
gpt4 key购买 nike

我正在尝试编写一个通用模板类,但在尝试实现它时我一直收到此错误:

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;
};

最佳答案

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

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

因此为Bar 提供一个默认的构造函数。例如:

class Bar {

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

关于c++ - 类模板中没有对 T::T() 的匹配函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com