继承和虚函数

时间:2016-12-31 21:05:29

标签: c++ virtual

我有一个Tableau类,其中包含虚函数updateCustom()和另一个基本函数checkNeighbours()。 我有另一个派生自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);

1 个答案:

答案 0 :(得分:1)

Type**投射到const Type**是非法的。

请参阅http://c-faq.com/ansi/constmismatch.html了解原因。