继承和虚拟功能

Inheritance and virtual function

本文关键字:功能 虚拟 继承      更新时间:2023-10-16

我有一个具有虚拟函数updateCustom()和另一个基本函数checkneighbours()的class 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**

是非法的。

请参阅http://c-faq.com/ansi/constmismatch.html有关原因的解释。