c++ - 错误 : no matching function for call

标签 c++ class constructor arguments

我正在调用一个单参数构造函数,但我收到一个错误,似乎是我正在调用一个无参数构造函数(它不存在也不应该存在)。

这是我遇到的错误

g++ -g -c predictor.C
In file included from predictor.C:5:
PHT.C: In constructor 'PHT::PHT(int)':
PHT.C:5: error: no matching function for call to'TwoBitPredictorTable::TwoBitPredictorTable()'
TwoBitPredictorTable.C:5: note: candidates are: TwoBitPredictorTable::TwoBitPredictorTable(int)
predictor.h:25: note: TwoBitPredictorTable::TwoBitPredictorTable(const TwoBitPredictorTable&)

这是 PHT.C 中第 5 行的构造函数调用

PHT::PHT(int rows)
{
    predictor = TwoBitPredictorTable(rows);
}

PHT 的类定义是:

class PHT
{
TwoBitPredictorTable predictor;

public:
    PHT(int rows);
    bool update(unsigned int pc, unsigned int ghr, bool outcome);
    bool getPrediction(unsigned int pc, unsigned int ghr);
};

想法是制作一个包装 TwoBitPredictorTable 的类 PHT。

我是 C++ 的新手,但经过数小时的寻找答案后,我请求您的帮助。提前致谢:)

最佳答案

需要调用初始化列表中的构造函数。你现在拥有的相当于:

PHT::PHT(int rows) :
    predictor() // <-- error, no default constructor
{
    predictor = TwoBitPredictorTable(rows);
}

相反:

PHT::PHT(int rows) :
    predictor(rows)
{
}

关于c++ - 错误 : no matching function for call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33223139/

相关文章:

c++ - 将 MYSQL_TIME 数据类型转换为 char * 或 C++ 字符串

c++ - 相同大小的 int 与 BGRA 并集产生不同的结果

c# - 使用字符串变量设置 'Class = new <string of Class()>'

带 header 的 C++ 委托(delegate)构造函数

JavaScript 对象创建/关联性

c++ - gcc下如何调用C编译器

c++ - 我正在访问什么错误的内存导致段错误?

java - 在 JVM 运行时重新加载类

python - 类型错误 : generatecode() takes 0 positional arguments but 1 was given

Java类构造函数