How do I use Derived
class in Base
class?
EDIT 2:
When calling virtual void move(Derived1 &race);
in main, it doesn't compile, but throws error that Derived1 is not found
. When I to call the function without the Derived1
class object, it does compile, but the function doesn't seem to do anything. Is it possible to get that function to work?
EDIT :
Base class
class Base {
protected:
int x, y;
int moves;
char name;
bool free;
public:
Base();
virtual void move(Derived1 &race);
};
Derived class 1
class Derived1 : public Base {
private:
const int x = 10;
const int y = 60;
Base ***track;
public:
Derived1(int x = 10, int y = 60);
void printBoard();
Base ***getArray() const;
~Derived1();
void move{}
};
Derived class 2
class Derived2 : public Base {
public:
Derived2();
Derived2(int x, int y);
void move(Derived1& race);
};
void move() function of Derived 2
It checks for obstacles in array. If free space is found it moves there. The code is really bad, since I haven't finished it, just a temporary code before I get everything going smoothly.
void Derived2::move(Derived1& race) {
if (race.getArray()[x][y + 1]->getFree() == true) {
delete[] race.getArray()[x][y];
race.getArray()[x][y] = new Base();
}
if (race.checkFreeY(x, y + 1, 3) == true) {
delete[] race.getArray()[x][y + 4];
race.getArray()[x][y + 4] = new Derived2(x, y + 4);
moves++;
}
else if (race.checkFreeX(x, y + 1, 3) == true) {
delete[] race.getArray()[x + 3][y + 1];
race.getArray()[x + 3][y + 1] = new Derived2(x + 3, y + 1);
moves++;
}
else {
moves++;
}
}
The assignment was to make a race with a virtual move()
function used in every other Derived class, that moves the object in a 2D array with obstacles.
EDIT 3:
The call I'm trying to make:
Derived1 track;
track.fillTrack();
track.genBushes();
track.genRacers();
track.getArray()[0][0]->move(track);
When doing this, I get following errors:
syntax error: identifier 'Derived1'
'void Base::move(Derived1&)': overloaded member function not found in 'Base'
"void Base::move(<error-type> &race)"
Editing move function in Base
as follows virtual void move() {}
and trying to call like track.getArray()[0][0]->move();
I'm getting following error:
too few arguments in function to call