I have a function that takes a reference to an interface class as a parameter. I have created a derived class from the interface class, but when I try to call the function with an instance of my derived class as an argument I get a compiler error saying "error: no matching call to 'FunctionName(DerivedClassName &)'" Why won't my compiler let me use inheritance?i
jackmaverick1 -4 Junior Poster
Could you post the code?
Labdabeta 182 Posting Pro in Training Featured Poster
Here is the specific code:
PathFindingAI.h:
#include <vector>
#include <limits.h>
using namespace std;
struct GameMove
{
unsigned char *data;
int l;
};
struct MoveScore
{
GameMove move;
int score;
};
class GameState
{
public:
virtual GameState &execute(GameMove)=0;//execute the move
virtual GameState &undo(GameMove)=0;//undo the move
virtual unsigned char *data()=0;
};
class GamePlayer
{
public:
virtual int evaluate(GameState&)=0;//evaluate the move
virtual vector<GameMove> getMoves(GameState&)=0;
};
template <unsigned int ply>
MoveScore AlphaBeta(GameState ¤t, GamePlayer &us, GamePlayer &them, int low=INT_MIN, int high=INT_MAX)
{
MoveScore best={{NULL,0},0};
if (ply==0||us.getMoves(current).size()==0)
{
best.score=us.evaluate(current);
return best;
}
vector<GameMove> moves=us.getMoves(current);
for (int i=0; i<moves.size(); i++)
{
current.execute(moves[i]);
MoveScore tmp=AlphaBeta<ply-1>(current,them,us,-high,-low);
current.undo(moves[i]);
if (-tmp.score>best.score)
{
low=-tmp.score;
best.move=moves[i];
best.score=low;
}
if (low>=high)
return best;
}
return best;
}
TicTacToeTest.cpp:
#include <iostream>
#include "PathFindingAI.h"
class TicTacToeState:public GameState
{
private:
unsigned char board[9];
public:
TicTacToeState(){board={' ',' ',' ',' ',' ',' ',' ',' ',' '};}
virtual GameState &execute(GameMove m)
{
if (m.l!=2||(m.data[0]!='X'&&m.data[0]!='O')||m.data[1]>8)
return *this;
board[m.data[1]]=m.data[0];
return *this;
}
virtual GameState &undo(GameMove m)
{
if (board[m.data[1]]!=m.data[0])
return *this;
board[m.data[1]]=' ';
return *this;
}
unsigned char *output()
{
unsigned char *ret=new unsigned char[10];
for (int i=0; i<9; i++)
ret[i]=board[i];
ret[9]=0;
return ret;
}
virtual unsigned char *data(){return board;}
};
class TicTacToePlayer:public GamePlayer
{
private:
bool isX;
public:
TicTacToePlayer(bool isX):isX(isX){};
virtual int evaluate(GameState &s)
{
unsigned char tiars[8][3]={ {s.data()[0],s.data()[1],s.data()[2]},
{s.data()[3],s.data()[4],s.data()[5]},
{s.data()[6],s.data()[7],s.data()[8]},
{s.data()[0],s.data()[4],s.data()[8]},
{s.data()[2],s.data()[4],s.data()[6]},
{s.data()[0],s.data()[3],s.data()[6]},
{s.data()[1],s.data()[4],s.data()[7]},
{s.data()[2],s.data()[5],s.data()[8]}};
int ret=0;
for (int i=0; i<8; i++)
{
//1st check for win
if (tiars[i][0]==tiars[i][1]&&tiars[i][1]==tiars[i][2])
{
if (isX)
return (tiars[i][0]=='X'?INT_MAX:INT_MIN);
else
return (tiars[i][0]=='X'?INT_MIN:INT_MAX);
}//then return [# of 3-in-a-rows left for me]-[# of 3-in-a-rows left for you]
else
{
if (tiars[i][0]==' '||tiars[i][1]==' '||tiars[i][2]==' ')
{
if (!(tiars[i][0]=='X'||tiars[i][1]=='X'||tiars[i][2]=='X'))//O can get this row!
(isX?ret--:ret++);
if (!(tiars[i][0]=='O'||tiars[i][1]=='O'||tiars[i][2]=='O'))//X can get this row!
(isX?ret++:ret--);
}
}
}
return ret;
}
virtual vector<GameMove> getMoves(GameState &s)
{
vector<GameMove> ret;
for (int i=0; i<9; i++)
{
if (s.data()[i]==' ')
{
GameMove m;
m.data=new unsigned char[2];
m.data[0]=(isX?'X':'O');
m.data[1]=i;
ret.push_back(m);
}
}
return ret;
}
};
int main()
{
TicTacToeState board;
TicTacToePlayer x(true),o(false);
bool isX=true;
while (x.getMoves(board).size()>0)//if X can go so can O
{
cout<<board.output();
if (isX)
board.execute(AlphaBeta(board,x,o).move);//here an error
else
board.execute(AlphaBeta(board,o,x).move);//and here an error
}
return true;
}
The errors say: error: no matching function for call to 'AlphaBeta(TicTacToeState&,TicTacToePlayer&,TicTacToePlayer&)'
Labdabeta 182 Posting Pro in Training Featured Poster
I figured it out. Facepalm kind of situation. AlphaBeta is a template function. I never specified the ply!
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.