-4

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

randomehh
  • 27
  • 8
  • 1
    why does a base class needs to get an instance of a derived class? – David Haim Dec 21 '15 at 10:43
  • Question is not that clear... Please add a snippet of how you're trying to use `Derived`. – Mr. Anderson Dec 21 '15 at 10:46
  • `Derived` has a 2D Dynamic array of pointers. I have to call it in order to use it. Forgot to add that in the code. – randomehh Dec 21 '15 at 10:46
  • @randomehh best way is to override parent method in child – roottraveller Dec 21 '15 at 10:48
  • 2
    Real code is almost always needed. – Jonathan Potter Dec 21 '15 at 10:49
  • What is the code that calls `move`? Also, when you edited the extra info into your question, you seem to have edited out the question. What are you asking? – JSF Dec 21 '15 at 11:07
  • Sorry, will edit in a second. – randomehh Dec 21 '15 at 11:17
  • What you showed above inside Derived1 `void move{}` is that really what the code looks like? Also, do describe the problem call to `move`, show us that call (and exact error message for the version that didn't compile). – JSF Dec 21 '15 at 12:07
  • Yes, `Derived1` move function doesn't do anything, and it shouldn't, well, It should have been removed anyway. Will that in the question in a second. – randomehh Dec 21 '15 at 12:10
  • Before defining `Base` you need to pre declare `class Derived1;` I think there is a lot more wrong, but you still aren't showing complete enough nor correct enough information to let anyone match the errors to the code. After adding that pre declare, maybe the error messages will change to something more useful. – JSF Dec 21 '15 at 12:25
  • Thank you, worked perfectly! – randomehh Dec 21 '15 at 12:33

1 Answers1

0

Before the definition of Base you need to say

class Derived1;

This will make the declaration of move valid:

virtual void move(Derived1 &race);

You will need to provide definitions of Base::move and Derived1::move (you can removed the need for Base::move by marking it pure virtual).

  • Thank you! This fixed it, guessing this isn't really good practice to do this, but well, my code is bad practice at it's best :D – randomehh Dec 21 '15 at 12:36