继承和虚函数
我有一个具有虚函数updateCustom()和另一个基本函数checkNeighbours()的Tableau类。 我有另一个从Tableau派生的类,JeuTaquin,覆盖updateCustom()。在updateCustom中,我想调用checkNeighbours(),但是出现错误。继承和虚函数
我的两个功能我班的Tableau内:
template<class T>
void updateCustom(char input) //Virtual function in .h
{}
template<class T>
Case<T>* Tableau<T>::checkNeighbours(const Case<T> **&plateau, int i, int j)
{
//No need to see what is inside this function
Case<T> *neighbours;
neighbours = new Case<T>[4];
for(int n = 0; n<taille;n++)
neighbours[n] = nullptr;
if(i!=0)
neighbours[0] = plateau[i-1][j];
if(j!=taille)
neighbours[1] = plateau[i][j+1];
if(i!=taille)
neighbours[2] = plateau[i+1][j];
if(j!=0)
neighbours[3] = plateau[i][j-1];
return neighbours;
}
内。然后JeuTaquin(从的Tableau派生):
template<class T>
void JeuTaquin<T>::updateCustom(char input)
{
//Here is my function checkNeighbours that I want to call
Case<T> *neighbours = Tableau<T>::checkNeighbours(Tableau<T>::plateau1, 2, 2);
}
当我尝试编译,我得到:
JeuTaquin.cpp:54:71: error: no matching function for call to ‘JeuTaquin<int>::checkNeighbours(Case<int>**&, int, int)’
neighbours = Tableau<T>::checkNeighbours(Tableau<T>::plateau1, 2, 2);
JeuTaquin.cpp:54:71: note: candidate is:
In file included from JeuTaquin.h:5:0,
from JeuTaquin.cpp:1:
Tableau.h:78:11: note: Case<T>* Tableau<T>::checkNeighbours(const Case<T>**&, int, int) [with T = int]
Case<T>* checkNeighbours(const Case<T> **&plateau, int i, int j);
Tableau.h:78:11: note: no known conversion for argument 1 from ‘Case<int>**’ to ‘const Case<int>**&’
我不知道为什么我无法识别我的优先updateCustom()内的checkNeighbours。我的包括都可以,我甚至在JeuTaquin的构造函数中调用Tableau中的函数,它运行良好!感谢您的帮助
编辑:我宣布在Tableau.h我updateCustom功能是这样的:
virtual void updateCustom(char input);
做法是非法的投Type**
到const Type**
。
我删除了const,它的工作,谢谢! –
把const的正确性抛出窗口来解决'**&'乱码造成的问题对我来说似乎是一个坏习惯。 – Unimportant
该问题与该参考无关。处理指针指针时保持const正确性比处理常规指针要困难得多。真正的问题是首先使用指针指针,但这是一个不同的故事。 – Frank
“checkNeighbours”是如何声明,公开,私有或受保护的?如果它是私密的,则不能从其他课程访问它。 – Barmar
checkNeighbours是公开的 –
你的'updateCustom'看起来很奇怪,它的模板和非虚拟的并不清楚,如果它的方法或免费函数,你可以添加更多的代码吗? –