我正在调用一个单参数构造函数,但我收到一个错误,似乎是我正在调用一个无参数构造函数(它不存在也不应该存在)。
这是我遇到的错误
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/