error: no matching function for call

error: no matching function for call

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

这是我遇到的错误

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 定义是:

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

我们的想法是制作一个 class PHT,它包装了一个 TwoBitPredictorTable。

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

看起来 TwoBitPredictorTable 没有默认构造函数。在 PHT 构造期间,您应该使用初始化列表来构造 TwoBitPredictorTable

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

应该看起来像这样。

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

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

改为:

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