继承和虚函数
Inheritance and virtual function
我有一个 class 带有虚函数 updateCustom() 和另一个基本函数 checkNeighbours() 的 Tableau。
我有另一个 class 来自 Tableau 的 JeuTaquin,它覆盖了 updateCustom()。在 updateCustom 中,我想调用 checkNeighbours() 但出现错误。
我的 class 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。
我有一个 class 带有虚函数 updateCustom() 和另一个基本函数 checkNeighbours() 的 Tableau。 我有另一个 class 来自 Tableau 的 JeuTaquin,它覆盖了 updateCustom()。在 updateCustom 中,我想调用 checkNeighbours() 但出现错误。
我的 class 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。