错误:没有匹配的呼叫功能

时间:2015-10-19 20:15:39

标签: 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 ++很陌生,但经过几个小时的寻找答案后,我一直在寻求你的帮助。在此先感谢:)

2 个答案:

答案 0 :(得分:2)

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

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

相反:

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

答案 1 :(得分:1)

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

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

应该看起来像这样。

相关问题